由于文件编写安全问题,我们尝试将应用程序从使用ms-appx-web:切换为使用ms-appdata:。但它立即失败,因为我们依赖于window.external.notify(),它可以与ms-appx-web一起工作:但似乎表现为ms-appdata的无操作:作为测试,我们将以下html加载到WebView对象中:
<html>
<head>
<script>
function demofunc( str ) {
document.getElementById("demo").innerHTML += str;
window.external.notify( str );
}
</script>
</head>
<body onLoad="demofunc('demofunc()');">
demo <body>
<div id="demo"></div>
</body>
</html>
产生这个结果:
demo <body>
demofunc()
但是,不会产生任何类型的弹出消息。为什么?显然,调用demofunc()方法在demo div中添加第二行输出,但window.external.notify()不会产生弹出消息。有没有关于notify()和ms-appdata的特殊规则:?
更新 - 问题Can't run javascript alerts in universal app's webview at payment gateway类似,适用于ms-appx-web:但不适用于ms-appdata:。该问题捕获ScriptNotify(),然后使用Windows.UI.Popups.MessageDialog弹出一个对话框。使用ms-appx-web:调用ScriptNotify()但使用ms-appdata:不调用ScriptNotify()。这是我们的问题,没有弹出窗口。
答案 0 :(得分:1)
但它立即失败,因为我们依赖于window.external.notify(),它可以与ms-appx-web一起工作:但似乎表现为ms-appdata的无操作:。
对于您的方案,请使用方案ms-local-stream:///
,而不是ms-appdata:///
。我已经测试了ms-local-stream:///
方案,它运行得很好。
要使用NavigateToLocalStreamUri
方法,您必须传入一个将URI模式转换为内容流的IUriToStreamResolver实现。请参考以下代码。
<强> StreamUriWinRTResolver 强>
public sealed class StreamUriWinRTResolver : IUriToStreamResolver
{
/// <summary>
/// The entry point for resolving a Uri to a stream.
/// </summary>
/// <param name="uri"></param>
/// <returns></returns>
public IAsyncOperation<IInputStream> UriToStreamAsync(Uri uri)
{
if (uri == null)
{
throw new Exception();
}
string path = uri.AbsolutePath;
// Because of the signature of this method, it can't use await, so we
// call into a separate helper method that can use the C# await pattern.
return getContent(path).AsAsyncOperation();
}
/// <summary>
/// Helper that maps the path to package content and resolves the Uri
/// Uses the C# await pattern to coordinate async operations
/// </summary>
private async Task<IInputStream> getContent(string path)
{
// We use a package folder as the source, but the same principle should apply
// when supplying content from other locations
try
{
Uri localUri = new Uri("ms-appdata:///local" + path);
StorageFile f = await StorageFile.GetFileFromApplicationUriAsync(localUri);
IRandomAccessStream stream = await f.OpenAsync(FileAccessMode.Read);
return stream.GetInputStreamAt(0);
}
catch (Exception) { throw new Exception("Invalid path"); }
}
}
<强>的MainPage 强>
public MainPage()
{
this.InitializeComponent();
MyWebView.ScriptNotify += MyWebView_ScriptNotify;
Uri url = MyWebView.BuildLocalStreamUri("MyTag", "/Test/HomePage.html");
StreamUriWinRTResolver myResolver = new StreamUriWinRTResolver();
MyWebView.NavigateToLocalStreamUri(url, myResolver);
}
我已将code sample上传到github。请检查。