【发布时间】: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