无法在支付网关的通用应用程序的 web 视图中运行 javascript 警报 [英] Can't run javascript alerts in universal app's webview at payment gateway
问题描述
我将支付网关集成到我的通用 Windows 应用程序中,我在 Web 视图中打开 URL.但是,webview 似乎无法显示 javascript 警报消息或弹出窗口.我在网上读到我需要在包清单中添加网站的 url 才能运行 Scriptnotify 事件,但鉴于它是一个支付网关,为所有银行网站添加 url 是不可行的.有没有办法解决这个问题?
I am integrating payment gateway in my universal windows app where I open the URL in a webview. However, webview can't seem to display javascript alert messages or popups. I read online that I need to add url's of website in package manifest to enable Scriptnotify event to run but given it's a payment gateway, it's not feasible to add url's for all bank websites. Is there a way to handle this ?
我也是这样处理 ScriptNotify 事件的,但这似乎是部分正确的.
Also I am handling the ScriptNotify event as this, but this seems to be partially correct.
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)}" });
}
推荐答案
折腾了一两个星期,终于找到了解决方案,以下是必要的事件处理程序:
After banging my head for a week or two, finally found the solution, following are the necessary event handlers :
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}" });
}
}
这篇关于无法在支付网关的通用应用程序的 web 视图中运行 javascript 警报的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!