1

WebView.Navigate(URI) 不会加载我的 uri 指向的 html 文件。PathToIndex 来自StorageFile.Path.

private static readonly Uri HomeUri = new Uri("ms-appx-web:///" + PathToIndex, UriKind.Absolute);

我要加载的 html 来自我之前解压缩的 Zip 文件。
有什么建议么?

编辑 Jogy 的回答:这是我解压缩 zip 文件的地方:

var localFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
var unpackFolder = await localFolder.CreateFolderAsync(appName+"unzip", CreationCollisionOption.ReplaceExisting);

然后我将 index.html 路径保存为StorageFile.Path.
我尝试了 ms-appdata,但出现value does not fall within the expected range异常
我可以遍历所有文件

var files = await unpackFolder.GetFilesAsync();
foreach(var file in files)
{
   string name = file.Path;
}


使用 NavigateToString 我也可以看到 Html 文件,但是 javascript 和 css 仍然不起作用。

4

3 回答 3

1

ms-appx-web 不起作用,因为它只能用于从应用程序包中加载资源。

我尝试了 ms-appdata 然后搜索了文档,结果是:

WebView 不支持 ms-appdata 方案,但它支持 ms-appx-web 方案,您可以使用它从应用程序包中加载内容文件。

一种解决方法是读取字符串中的文件内容,然后使用 WebView.NavigateToString()。如果您有 html 的外部资源,您还可以探索 NavigateToLocalStreamUri()。

于 2014-09-15T14:47:51.533 回答
1

这将帮助你...... https://code.msdn.microsoft.com/windowsapps/XAML-WebView-control-sample-58ad63f7/

我的例子:

StorageFolder d = await ApplicationData.Current.LocalFolder.CreateFolderAsync("folder", CreationCollisionOption.OpenIfExists);
StorageFile f = await d.CreateFileAsync("index.html", CreationCollisionOption.ReplaceExisting);
Stream str = await f.OpenStreamForWriteAsync();
using (StreamWriter sw = new StreamWriter(str))
{
  sw.Write("<html>...</html>");
}
string url = "ms-appdata:///local/folder/index.html";
webView1.Navigate(new Uri(url));
于 2015-05-17T15:52:41.970 回答
1

为了澄清 NeoSvet 的回答:ms-appdata仅当您将 html 文件存储在 LocalFolder 的子文件夹中而不是在根目录中时才有效,如下所示:"ms-appdata:///local/[subfolder_name]/index.html".

其原因在此处进行了详细描述,简而言之:这样做是为了为 Web 内容提供某种程度的隔离,以便 WebView 不会意外暴露本地文件夹中的其他内容。

于 2016-08-08T10:50:03.337 回答