我正在我的通用Windows应用程序中集成支付网关,我在webview中打开URL。但是,webview似乎无法显示javascript警报消息或弹出窗口。我在网上看到我需要在软件包清单中添加网站的url以启用Scriptnotify事件,但鉴于它是支付网关,为所有银行网站添加网址是不可行的。有办法解决这个问题吗?
此外,我正在处理ScriptNotify事件,但这似乎是部分正确的。
private async void MyWebView_ScriptNotify(object sender, NotifyEventArgs e)
{
Windows.UI.Popups.MessageDialog dialog = new Windows.UI.Popups.MessageDialog(e.Value);
await dialog.ShowAsync();
}
private async void MyWebView_NavigationCompleted(WebView sender, WebViewNavigationCompletedEventArgs args)
{
string result = await this.MyWebView.InvokeScriptAsync("eval", new string[] { "window.alert = function (AlertMessage) {window.external.notify(AlertMessage)}" });
}
答案 0 :(得分:1)
在敲了一两个星期之后,终于找到了解决方案,以下是必要的事件处理程序:
private async void MyWebView_NavigationCompleted(WebView sender, WebViewNavigationCompletedEventArgs args)
{
string result = await this.MyWebView.InvokeScriptAsync("eval", new string[] { "window.confirm = function (ConfirmMessage) {window.external.notify(ConfirmMessage)}" });
}
private async void MyWebView_ScriptNotify(object sender, NotifyEventArgs e)
{
Windows.UI.Popups.MessageDialog dialog = new Windows.UI.Popups.MessageDialog(e.Value);
dialog.Commands.Add(new UICommand("Yes"));
dialog.Commands.Add(new UICommand("No"));
// Set the command that will be invoked by default
dialog.DefaultCommandIndex = 0;
// Set the command to be invoked when escape is pressed
dialog.CancelCommandIndex = 1;
var result = await dialog.ShowAsync();
if (result.Label.Equals("Yes"))
{
string res = await MyWebView.InvokeScriptAsync("eval", new string[] { "window.confirm = function (ConfirmMessage) {return true}" });
}
else
{
string res = await MyWebView.InvokeScriptAsync("eval", new string[] { "window.confirm = function (ConfirmMessage) {return false}" });
}
}