2017-04-18 102 views
0

由于文件写入安全问题,我们尝试将我们的应用程序从使用ms-appx-web切换到使用ms-appdata :.但是它会立即失败,因为我们依赖于window.external.notify(),它可以很好地处理ms-appx-web:但似乎像ms-appdata:一样无法运行。作为测试,我们加载以下HTML成的WebView对象:如何在Windows 10上使用ms-appdata通知Javascript()UWP

<html> 
<head> 
    <script> 
     function demofunc(str) { 
      document.getElementById("demo").innerHTML += str; 
      window.external.notify(str); 
     } 
    </script> 
</head> 
<body onLoad="demofunc('demofunc()');"> 
    demo &lt;body&gt; 
    <div id="demo"></div> 
</body> 
</html> 

产生这样的结果,因为它应该:

demo <body> 
demofunc() 

,但不会产生任何类型的弹出消息。为什么?显然demofunc()方法被调用来在演示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

也许这会帮忙吗? https://social.msdn.microsoft.com/Forums/zh-CN/bdde7d0f-b1dd-4708-95a9-afe1a50dbb75/script-notify-for-msappdata?forum=w81prevwCsharp – NewToJS

+0

可能重复[无法运行JavaScript警报在支付网关通用应用程序的webview](http://stackoverflow.com/questions/31804380/cant-run-javascript-alerts-in-universal-apps-webview-at-payment-gateway) – AVK

+0

谢谢你的链接,这很有趣。它似乎解决了捕获notify()事件并运行另一种方法,这确实非常有用。但是我们也确实需要为用户弹出一个对话框消息。缺少弹出对话框实际上是这篇文章的最重要的原因。 – user3334340

回答

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上。请检查。

+0

谢谢,这确实解决了弹出对话框不显示的问题。显然,UWP不允许Javascript使用ms-appdata启动弹出窗口,但允许使用ms-local-stream。 – user3334340