是否可以让 WebView 控件读取本地 html 文件? [英] Is it possible to make WebView control to read local html files?

查看:21
本文介绍了是否可以让 WebView 控件读取本地 html 文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个带有 WebView 控件的应用程序.我希望它读取本地 html 文件.但我找不到正确的方法让它成为可能.

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.

一开始,我只是简单地尝试使用Navigate方法,并在file:///~"格式字符串中提供文件路径作为参数,但没有奏效.

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

此 Microsoft 页面说 WebView 控件无法识别file:///~"协议.并展示了使WebView控件读取本地html文件的3种方案.

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. 使用 NavigateToLocal() 方法.
  2. 使用 NavigateToLocalStreamUri() 方法.
  3. 使用 NavigateToString() 方法.

我尝试了所有这些,但每个 3 都有一些问题,无法正常工作.

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

  1. NavigateToLocal 方法需要文件的相对路径(不是绝对路径),相对于应用程序可执行目录.因此,此方法无法读取应用程序目录中其他位置的文件.
  2. NavigateToLocalStreamUri 方法甚至没有根据页面实现!我曾经尝试过,但它返回了一个异常并且没有工作.
  3. NavigateToString 方法可以渲染给定的 html 内容字符串,但是 html 代码包含的 css、js、图像文件等外部文件无法加载,因此它没有提供完整的功能.

我找到了一些使用 NavigateToLocalStreamUri 方法的示例并自己尝试了.

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

通过这段代码,NavigateToLocalStreamUri 方法返回 System.Resources.MissingManifestResourceException.

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

我想实现的很简单.

  • 使用 WebView 控件
  • 读取位于本地存储中任意位置的本地 html 文件
  • 并按照预期的结果完全呈现 html 文件

但我现在看不清路.感谢您的建议或帮助.

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

推荐答案

NavigateToLocalStreamUri 方法将不起作用.请参阅 https://docs.microsoft.com/en-us/windows/communitytoolkit/controls/wpf-winforms/webview-known-issues.您必须使用 NavigateToLocal,但您会看到它已被弃用的警告.但是,它仅适用于相对路径.您是否可以重构您的应用程序以便您可以使用相对路径?

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?

NavigateToLocal 方法是我发现在 Microsoft.Toolkit.Forms.UI.Controls.WebView v6.0 中调用本地 HTML 文件的唯一方法.

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.

这篇关于是否可以让 WebView 控件读取本地 html 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆