Reputation: 31
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.
I tried all of them, but each 3 have some issues that doesn't make it work.
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.
But I don't see the way right now. I would appreciate your advises or helps.
Upvotes: 3
Views: 1939
Reputation: 3715
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.
Upvotes: 0
Reputation: 2016
The method NavigateToLocalStreamUri
will not work. Please see https://learn.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.
Upvotes: 1