You should leverage the class IUriToStreamResolver
.
In the following example, I load the HTML file (example.html) located in Assets
folder. The Assets
folder also contains all images of the HTML file.
The GetContent
method of IUriToStreamResolver
should be overriden to transform any request to an URI specified in the HTML file (such as ) into a local uri path. In your case, you may have to tweak the GetContent
so as to retrieves file stored by your resource map.
public sealed partial class WebViewLocalImage : Page
{
public WebViewLocalImage()
{
this.InitializeComponent();
Loaded += MainPage_Loaded;
}
private void MainPage_Loaded(object sender, RoutedEventArgs e)
{
var uri = this.myWebView.BuildLocalStreamUri("InternalAssets", "example.html");
this.myWebView.NavigateToLocalStreamUri(uri, new StreamUriResolver());
}
}
public sealed class StreamUriResolver : IUriToStreamResolver
{
public IAsyncOperation<IInputStream> UriToStreamAsync(Uri uri)
{
string path = uri.AbsolutePath;
return GetContent(path).AsAsyncOperation();
}
private async Task<IInputStream> GetContent(string URIPath)
{
try
{
Uri localUri = new Uri("ms-appx:///Assets/" + URIPath);
StorageFile f = await StorageFile.GetFileFromApplicationUriAsync(localUri);
IRandomAccessStream stream = await f.OpenAsync(FileAccessMode.Read);
return stream.GetInputStreamAt(0);
}
catch (Exception)
{
System.Diagnostics.Debug.WriteLine("Invalid URI");
}
return null;
}
}