为什么我无法在 Wpf 中使用应用程序内部的 WebView 导航 html 文件?
Why I cannot Navigate the html file by using WebView inside of the App in Wpf?
我制作了一个 Wpf 项目,它演示了如何使用 WebView 在应用程序中导航 html 文件,但失败了。
主要的cs文件代码如下:
public MainWindow()
{
this.InitializeComponent();
this.wv.ScriptNotify += Wv_ScriptNotify;
this.Loaded += MainPage_Loaded;
}
private async void Wv_ScriptNotify(object sender, Microsoft.Toolkit.Win32.UI.Controls.Interop.WinRT.WebViewControlScriptNotifyEventArgs e)
{
//await (new MessageDialog(e.Value)).ShowAsync();
textBlock.Text = e.Value;
//返回结果给html页面
await this.wv.InvokeScriptAsync("recieve", new[] { "hehe, 我是个结果" });
}
private void MainPage_Loaded(object sender, RoutedEventArgs e)
{
//我们事先写好了一个本地html页面用来做测试
this.wv.Source = new Uri("ms-appx-web://Assets/index.html");
//this.wv.Source = new Uri("http://www.baidu.com");
}
并且 html 文件 index.html 在项目内部,位于 Assets/index.html
。它的源代码在这里:
https://github.com/tomxue/WebViewIssueInWpf/raw/master/WpfApp3/Assets/index.html
我把项目代码放到GitHub:https://github.com/tomxue/WebViewIssueInWpf.git
如果项目运行良好,WebView访问内部html文件时,应该首先显示一个按钮。
但是我什么也没看到。
更多:
根据接受的答案(感谢 Pavel Anikhouski),我按如下方式更改了我的代码,现在可以使用了。
private void MainPage_Loaded(object sender, RoutedEventArgs e)
{
//我们事先写好了一个本地html页面用来做测试
//this.wv.Source = new Uri("ms-appx-web://Assets/index.html");
//this.wv.Source = new Uri("http://www.baidu.com");
var html = File.ReadAllText("../../Assets\index.html");
wv.NavigateToString(html);
}
好像是known issue在WindowsCommunityToolkit
中有WebView
控制
- You can use only absolute URIs to resources in members of the
WebView
control that accept string paths.
WebView
controls don't recognize the ms-appx:///
prefix, so they can't read from the package (if you've created a package for your
application).
WebView
controls don't recognize the File://
prefix. If you want to read a file into a WebView
control, add code to your application that
reads the content of the file. Then, serialize that content into a
string, and call the NavigateToString(String)
method of the WebView
control.
因此,不要加载文件 this.wv.Source = new Uri("ms-appx-web://Assets/index.html");
而是尝试读取本地文件,然后导航到字符串
var html = File.ReadAllText("Assets\index.html");
this.wv.NavigateToString(html);
它应该可以正常工作(我已经在我这边看到了按钮和消息)。另外,不要忘记将 Assets\index.html
复制到输出目录(设置 Copy Always 或 Copy if newer)
我制作了一个 Wpf 项目,它演示了如何使用 WebView 在应用程序中导航 html 文件,但失败了。
主要的cs文件代码如下:
public MainWindow()
{
this.InitializeComponent();
this.wv.ScriptNotify += Wv_ScriptNotify;
this.Loaded += MainPage_Loaded;
}
private async void Wv_ScriptNotify(object sender, Microsoft.Toolkit.Win32.UI.Controls.Interop.WinRT.WebViewControlScriptNotifyEventArgs e)
{
//await (new MessageDialog(e.Value)).ShowAsync();
textBlock.Text = e.Value;
//返回结果给html页面
await this.wv.InvokeScriptAsync("recieve", new[] { "hehe, 我是个结果" });
}
private void MainPage_Loaded(object sender, RoutedEventArgs e)
{
//我们事先写好了一个本地html页面用来做测试
this.wv.Source = new Uri("ms-appx-web://Assets/index.html");
//this.wv.Source = new Uri("http://www.baidu.com");
}
并且 html 文件 index.html 在项目内部,位于 Assets/index.html
。它的源代码在这里:
https://github.com/tomxue/WebViewIssueInWpf/raw/master/WpfApp3/Assets/index.html
我把项目代码放到GitHub:https://github.com/tomxue/WebViewIssueInWpf.git
如果项目运行良好,WebView访问内部html文件时,应该首先显示一个按钮。 但是我什么也没看到。
更多:
根据接受的答案(感谢 Pavel Anikhouski),我按如下方式更改了我的代码,现在可以使用了。
private void MainPage_Loaded(object sender, RoutedEventArgs e)
{
//我们事先写好了一个本地html页面用来做测试
//this.wv.Source = new Uri("ms-appx-web://Assets/index.html");
//this.wv.Source = new Uri("http://www.baidu.com");
var html = File.ReadAllText("../../Assets\index.html");
wv.NavigateToString(html);
}
好像是known issue在WindowsCommunityToolkit
WebView
控制
- You can use only absolute URIs to resources in members of the
WebView
control that accept string paths.WebView
controls don't recognize thems-appx:///
prefix, so they can't read from the package (if you've created a package for your application).WebView
controls don't recognize theFile://
prefix. If you want to read a file into aWebView
control, add code to your application that reads the content of the file. Then, serialize that content into a string, and call theNavigateToString(String)
method of theWebView
control.
因此,不要加载文件 this.wv.Source = new Uri("ms-appx-web://Assets/index.html");
而是尝试读取本地文件,然后导航到字符串
var html = File.ReadAllText("Assets\index.html");
this.wv.NavigateToString(html);
它应该可以正常工作(我已经在我这边看到了按钮和消息)。另外,不要忘记将 Assets\index.html
复制到输出目录(设置 Copy Always 或 Copy if newer)