如何在Windows 10 UWP上使用ms-appdata编写Javascript notify() [英] How to Javascript notify() with ms-appdata on Windows 10 UWP
问题描述
由于文件写入安全问题,我们试图将我们的应用程序从使用ms-appx-web:切换为使用ms-appdata:。但是它立即失败了,因为我们依赖于window.external.notify(),它在ms-appx-web:上可以正常工作,但在ms-appdata:上却表现为无操作。作为测试,我们将以下html加载到WebView对象中:
Due to file-writing security issues we have tried to switch our app from using ms-appx-web: to using ms-appdata:. But it immediately fails because we rely on window.external.notify() which works fine with ms-appx-web: but seems to behave as a no-op with ms-appdata:. As a test we loaded the following html into a WebView object:
<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的特殊规则??
BUT, does NOT produce a popup message of any kind. Why? Clearly the demofunc() method is being called to add the second line of output in the demo div, but window.external.notify() is not producing a popup message. Are there special rules regarding notify() along with ms-appdata:?
更新-问题无法在付款网关的通用应用程序的webview中运行javascript警报相似并且适用于ms-appx-网络:但不适用于ms-appdata:。该问题捕获了ScriptNotify(),该脚本随后使用Windows.UI.Popups.MessageDialog弹出对话框。对于ms-appx-web:调用ScriptNotify(),而对于ms-appdata:不调用ScriptNotify()。那就是我们的问题,没有弹出窗口。
Update - the question Can't run javascript alerts in universal app's webview at payment gateway is similar and works for ms-appx-web: but not for ms-appdata:. That question catches ScriptNotify() which then uses Windows.UI.Popups.MessageDialog to popup a dialog. With ms-appx-web: the ScriptNotify() is called but with ms-appdata: the ScriptNotify() is not called. That is our problem, there is no popup occurring.
推荐答案
但是它立即失败,因为我们依赖在window.external.notify()上可以在ms-appx-web上正常工作:但是在ms-appdata:上似乎表现为无操作:。
But it immediately fails because we rely on window.external.notify() which works fine with ms-appx-web: but seems to behave as a no-op with ms-appdata:.
对于您的情况,请使用方案 ms-local-stream:/// ,而不是
ms-appdata:// /
。而且我已经测试了 ms-local-stream:///
方案,它运行良好。
For your scenario please use the scheme ms-local-stream:///
, rather than ms-appdata:///
. And I have tested ms-local-stream:///
scheme, it is working pretty well.
到使用 <$ c在NavigateToLocalStreamUri 方法中,您必须传入IUriToStreamResolver实现,该实现将URI模式转换为内容流。请参考以下代码。
To use the NavigateToLocalStreamUri
method, you must pass in an IUriToStreamResolver implementation that translates a URI pattern into a content stream. Please reference the following code.
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"); }
}
}
主页
public MainPage()
{
this.InitializeComponent();
MyWebView.ScriptNotify += MyWebView_ScriptNotify;
Uri url = MyWebView.BuildLocalStreamUri("MyTag", "/Test/HomePage.html");
StreamUriWinRTResolver myResolver = new StreamUriWinRTResolver();
MyWebView.NavigateToLocalStreamUri(url, myResolver);
}
我已经上传了代码示例到github。请检查。
I have uploaded the code sample to github. Please check.
这篇关于如何在Windows 10 UWP上使用ms-appdata编写Javascript notify()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!