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.
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.
- Use NavigateToLocal() method.
- Use NavigateToLocalStreamUri() method.
- Use NavigateToString() method.
I tried all of them, but each 3 have some issues that doesn't make it work.
- 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.
- NavigateToLocalStreamUri method is not even implemented according to the page! I once tried it anyway, but it returned an exception and didn't work.
- 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.