Display local images in UWP WebView control

后端 未结 1 503
無奈伤痛
無奈伤痛 2020-12-31 22:39

I need to write the code which will load some HTML (received from external source by other means) into WebView and display images referenced in this HTML. These images will

相关标签:
1条回答
  • 2020-12-31 23:31

    Although NavigateToString supports content with references to external files such as CSS, scripts, images, and fonts. But it only supports content in the app package using the ms-appx-web scheme, and web content using the http and https URI schemes. It can't work with assets located in the apps local folder. So using file:///, ms-appdata:/// or ms-appx:/// scheme won't work here.

    To achieve what you want, you can use Base64 encoded images or custom URI resolver. Also I think you can store the html string to a file under the same subfolder that stores these image assets. In the html, you can use relative URI to reference these assets like what in my previous answer. And then use the Navigate method with a Uri that uses the ms-appdata scheme. For example:

    var html = "<html><head></head><body>A <img src=\"image001.jpg\"> B</body></html>";
    
    var folder = await ApplicationData.Current.LocalFolder.CreateFolderAsync("folder", CreationCollisionOption.OpenIfExists);
    var file = await folder.CreateFileAsync("html.html", CreationCollisionOption.OpenIfExists);
    await FileIO.WriteTextAsync(file, html);
    
    webView.Navigate(new Uri("ms-appdata:///local/folder/html.html"));
    
    0 讨论(0)
提交回复
热议问题