Dmy*_*tro 6 c# webview uwp uwp-xaml
我相信这是非常简单的任务,我在网上看到了很多例子,但是由于我遇到了不同的例外,它们中没有一个能为我工作.
HTML:
<html lang="en">
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<p>
Help html content.
</p>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
XAML:
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition Height="30"/>
</Grid.RowDefinitions>
<WebView x:Name="webView" />
<Button x:Name="buttonRefresh"
Grid.Row="1"
HorizontalAlignment="Center"
Click="buttonRefresh_Click">
Refresh
</Button>
</Grid>
Run Code Online (Sandbox Code Playgroud)
要help.html在我的UWP应用程序LocalFolder中显示保存在文件中的静态html ,我已经尝试过以下操作:
- 使用Navigate方法:
private void buttonRefresh_Click(object sender, RoutedEventArgs e)
{
var uri = new Uri("ms-appdata:///local/help.html");
webView.Navigate(uri);
}
Run Code Online (Sandbox Code Playgroud)
结果是以下异常:
System.ArgumentException: Value does not fall within the expected range.
at Windows.UI.Xaml.Controls.WebView.Navigate(Uri source)
at SimpleUwpApp.Proxy.SimplerPage.buttonRefresh_Click(Object sender, RoutedEventArgs e)
Run Code Online (Sandbox Code Playgroud)
- 尝试在后面的代码中显式设置webView的Source属性:
private void buttonRefresh_Click(object sender, RoutedEventArgs e)
{
var uri = new Uri("ms-appdata:///local/help.html");
webView.Source = uri;
}
Run Code Online (Sandbox Code Playgroud)
结果:
System.ArgumentException: Value does not fall within the expected range.
at Windows.UI.Xaml.Controls.WebView.put_Source(Uri value)
at SimpleUwpApp.Proxy.SimplerPage.buttonRefresh_Click(Object sender, RoutedEventArgs e)
Run Code Online (Sandbox Code Playgroud)
- 在xaml中显式设置webView的Source属性:这是microsoft文档中的确切示例.
<WebView x:Name="webView" Source="ms-appdata:///local/help.html" />
Run Code Online (Sandbox Code Playgroud)
因此在启动时出现异常:
Windows.UI.Xaml.Markup.XamlParseException: The text associated with this error code could not be found.
Failed to assign to property 'Windows.UI.Xaml.Controls.WebView.Source' because the type 'Windows.Foundation.String' cannot be assigned to the type 'Windows.Foundation.Uri'. [Line: 16 Position: 54]
at Windows.UI.Xaml.Application.LoadComponent(Object component, Uri resourceLocator, ComponentResourceLocation componentResourceLocation)
at SimpleUwpApp.Proxy.SimplerPage.InitializeComponent()
Run Code Online (Sandbox Code Playgroud)
- 尝试直接在Navigate()参数中使用url字符串,就像在这个微软示例中一样,但Navigate()只接受Uri作为一个参数,因此文档无效,无论是旧版本的xaml工具包.
webView.Navigate("ms-appx-web:///help.html");
Run Code Online (Sandbox Code Playgroud)
结果:
Syntax error.
Run Code Online (Sandbox Code Playgroud)
我临时提出的唯一解决方案是使用某种文件管理器和使用NavigateToString()方法读取html文件的内容:
var content = fileManager.Read("help.html"); // Manually read the content of html file
webView.NavigateToString(content);
Run Code Online (Sandbox Code Playgroud)
所以问题是为什么描述的例子不起作用?如何避免使用NavigateToString?
所以我有两件不同的东西,对我有用:
XAML版本:
<WebView x:Name="webView" Source="ms-appx-web:///help.html"/>
Run Code Online (Sandbox Code Playgroud)
在这里你需要使用ms-appx-web前缀.
代码隐藏版本:
webView.Navigate(new Uri("ms-appx-web:///help.html"));
Run Code Online (Sandbox Code Playgroud)
与您的版本的不同之处在于,您不能仅输入字符串.您需要创建一个新的Uri-Class 对象.
在这两个版本中,html-file是项目的直接子项,项目是Windows-10-UWP Application[你没有说,如果你使用Windows 8或Windows 10]
TheTanic提供的解决方案适用于随包提供的文件.对于存储在LocalFolder或TemporaryFolder中的文件,您必须遵循特殊的URI方案:
对此方案的WebView支持要求您将内容放在本地或临时文件夹下的子文件夹中.这样可以导航到统一资源标识符(URI),例如ms-appdata:///local/folder/file.html和ms-appdata:///temp/folder/file.html
这意味着如果您使用LocalFolder,那么URI将如下所示:ms-appdata:///local/此后必须是文件夹并在文件中.这是一个工作样本:
StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///help.html"));
StorageFolder folder = await ApplicationData.Current.LocalFolder.CreateFolderAsync("anyFolder", CreationCollisionOption.OpenIfExists);
await file.CopyAsync(folder, "help.html", NameCollisionOption.ReplaceExisting);
webView.Navigate(new Uri("ms-appdata:///local/anyFolder/help.html"));
Run Code Online (Sandbox Code Playgroud)
另请注意,所有使用的内容也必须位于该文件夹中:
这些第一级子文件夹中的每一个都与其他第一级子文件夹中的内容隔离.