Ich verwende NavigateToLocalStreamUri
in WebView
, damit ich bestimmte Stylesheets für die hellen und dunklen Themen laden kann. I-Format meine HTML-Dateien wie folgt:NavigateToLocalStreamUri nicht für Ressourcen aufgerufen
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<link rel="stylesheet" type="text/css" href="css/main.css" />
<link rel="stylesheet" type="text/css" href="css/theme-(RequestedTheme).css" />
</head>
<body>
...
</body>
</html>
Hier ist mein StreamUriResolver
Code ist wie folgt:
public IAsyncOperation<IInputStream> UriToStreamAsync(Uri uri)
{
if (uri == null)
{
throw new ArgumentException("Uri cannot be empty");
}
string path = uri.AbsolutePath;
// Because of the signature of the this method, it can't use await, so we
// call into a seperate helper method that can use the C# await pattern.
return GetContent(path).AsAsyncOperation();
}
private async Task<IInputStream> GetContent(string path)
{
// We use a package folder as the source, but the same principle should apply
// when supplying content from other locations
try
{
// Replace content modifiers with actual values
path = path.Replace("(RequestedTheme)", Application.Current.RequestedTheme.ToString().ToLowerInvariant());
Uri localUri = new Uri("ms-appx:///Content" + path);
StorageFile f = await StorageFile.GetFileFromApplicationUriAsync(localUri);
IRandomAccessStream stream = await f.OpenAsync(FileAccessMode.Read);
return stream.GetInputStreamAt(0);
}
catch (Exception) { throw new InvalidOperationException("Invalid path"); }
}
Und es wird mit diesem Code genannt:
Uri uri = webViewer.BuildLocalStreamUri("Landing", url);
StreamUriResolver myResolver = new StreamUriResolver();
// Pass the resolver object to the navigate call.
webViewer.NavigateToLocalStreamUri(uri, myResolver);
ich einen Haltepunkt hinzugefügt in der UriStreamResolver
, aber GetContent
wird nur einmal auf der HTML-Seite und nicht für die Stylesheets oder Bilder auf der Seite getroffen. Sie werden geladen und das Bild wird angezeigt, aber das bedingte Stylesheet wird nicht geladen. Die Artikel und Beiträge, die ich gelesen habe, sagen, dass GetContent
jedes Mal aufgerufen werden sollte, wenn eine Ressource geladen werden muss, aber ich sehe dieses Verhalten nicht.
Haben Sie die 'css'-Datei im' Content'-Ordner abgelegt? –
Ja, alle Dateien befinden sich im Ordner "Content". –