Is it possible to make WebView control to read local html files?

拈花ヽ惹草 提交于 2021-01-27 12:11:43

问题


I'm making an application with WebView control. And I want it to read local html file. But I can't find the right way to make it possible.

At first, I simply tried to use Navigate method and provide the file path in the "file:///~" format string as a parameter, but it didn't work.

https://docs.microsoft.com/ja-jp/windows/communitytoolkit/controls/wpf-winforms/webview-known-issues

This Microsoft page says that WebView control does not recognize "file:///~" protocol. And it shows the 3 solutions to make WebView control to read local html files.

  1. Use NavigateToLocal() method.
  2. Use NavigateToLocalStreamUri() method.
  3. Use NavigateToString() method.

I tried all of them, but each 3 have some issues that doesn't make it work.

  1. NavigateToLocal method requires a RELATIVE path of the file (not the absolute path), relative from the application executable directory. So files in somewhere else from the application directory cannot be read by this method.
  2. NavigateToLocalStreamUri method is not even implemented according to the page! I once tried it anyway, but it returned an exception and didn't work.
  3. NavigateToString method can render the given html content string, but the external files like css, js, image files included by html codes cannot be loaded, so it does not provide a full function.

I found some sample of using NavigateToLocalStreamUri method and tried it by myself.

(VB.NET)

wvwMain.NavigateToLocalStreamUri(uri, New StreamUriResolver())


Public Class StreamUriResolver : Implements IUriToStreamResolver
    Public Function UriToStream(uri As Uri) As Stream Implements IUriToStreamResolver.UriToStream
        Return New FileStream(uri.LocalPath, FileMode.Open)
    End Function
End Class

By this code, NavigateToLocalStreamUri method returns System.Resources.MissingManifestResourceException.

What I want to realize is very simple.

  • Using WebView control
  • Read local html file located anywhere on the local storage
  • And render the html file completely as an expected result

But I don't see the way right now. I would appreciate your advises or helps.


回答1:


The method NavigateToLocalStreamUri will not work. Please see https://docs.microsoft.com/en-us/windows/communitytoolkit/controls/wpf-winforms/webview-known-issues. You have to use NavigateToLocal, but you will see a warning that it is deprecated. However, it does only work with relative paths. Is it possible for you to restructure your application so that you can use relative paths?

The NavigateToLocal method is the only way that I've found to call local HTML files in Microsoft.Toolkit.Forms.UI.Controls.Web WebView v6.0.




回答2:


On Visual Studio 2019 Windows 10, the following VB.NET code works on my PC

Imports System.IO

Dim sFileName = Application.StartupPath & "/MyPage.html"
wv.NavigateToString(System.IO.File.ReadAllText(sFileName))

where wv is a WebView object.



来源:https://stackoverflow.com/questions/55516387/is-it-possible-to-make-webview-control-to-read-local-html-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!