Kats Sakuma
Kats Sakuma

Reputation: 31

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

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://learn.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.

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

Upvotes: 3

Views: 1939

Answers (2)

schlebe
schlebe

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

GamerDev
GamerDev

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

Related Questions