【问题标题】:UWP Webview ScriptNotify is not getting trigerredUWP Webview 脚本通知未触发
【发布时间】:2020-04-15 23:04:25
【问题描述】:
Need to get callback from webpage to my "WebView" in C# using scriptnotify. But it's not working. 

我使用节点 js 在本地托管了一个网页。网页如下。

<!DOCTYPE html>
<html>
<body>
<h1>The onclick Event</h1>
<button onclick="myFunction()">Click me</button>
<p id="demo"></p>
<script> 
function myFunction() {
  document.getElementById("demo").innerHTML = "latest";
  window.external.notify('The script says the doubled value is ' );
}
</script>
</body>
</html>

之后创建了一个 UWP 应用来使用 webview 打开网页。在 JS 中添加 window.external.notify 以获取回调。但不一样。 //获取回调的C#代码 公共密封部分类 MainPage : Page { 公共主页() {
this.InitializeComponent(); //在此处添加通知处理程序 webView.ScriptNotify += webView_ScriptNotify; //导航到本地页面 webView.Navigate(new Uri("http://localhost:8080/")); }

        async void webView_ScriptNotify(object sender, NotifyEventArgs e)
        {
            //Kept break point here , but it's not getting hit any time. 
            var jsScriptValue = e.Value;
            Debug.WriteLine("Send to debug output.");
        }
    }   

 //Added a web view control in xaml file
 <Grid>
    <WebView x:Name="webView" Height="500"> </WebView>
</Grid>

【问题讨论】:

    标签: javascript c# webview uwp callback


    【解决方案1】:

    UWP Webview ScriptNotify 没有被触发

    请查看WebViewdocument

    要使外部网页能够在调用 window.external.notify 时触发 ScriptNotify 事件,您必须在应用清单的 ApplicationContentUriRules 部分中包含该页面的 URI。 (您可以在 Microsoft Visual Studio 中 Package.appxmanifest 设计器的 Content URIs 选项卡上执行此操作。)此列表中的 URI 必须使用 HTTPS,并且可能包含子域通配符(例如 https://.microsoft。 com),但它们不能包含域通配符(例如,https://.com 和 https://.)。清单要求不适用于源自应用程序包、使用 ms-local-stream:// URI 或使用 NavigateToString 加载的内容。

    因此,您需要将您的 uri 添加到内容 uri 选项中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-28
      相关资源
      最近更新 更多