UWP Webview ScriptNotify不会被触发

问题描述 投票:0回答:1
Need to get callback from webpage to my "WebView" in C# using scriptnotify. But it's not working. 

我使用node 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:页面{公共MainPage(){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个回答
0
投票

UWP Webview ScriptNotify不会被触发

请检查WebView document

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

因此,您需要将uri添加到content uri选项。

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.