繁体   English   中英

如何处理WebView中的确认对话框? UWP Windows 10应用程序C#

[英]How can i handle confirm dialog in webview? UWP windows 10 app C#

我正在用C#开发uwp应用程序(win 10)

我想将我的网站放在xaml webview元素中。

大多数功能都是可行的。

但我无法处理确认对话框

例如

这是示例html&js代码

<button id="btnConfirm" onclick="confirmBox('sure to delete?')">click me to confirm</button>
<button id="btnAlert" onclick="alert('alert message')">click me to alert</button>
<script type="text/javascript">
    function confirmBox(message) {
        if (confirm(message)) {
         alert("yes");
      } else {
         alert("no");
      }
    }
</script>

这是我的XAML代码

<WebView Grid.Row="1" x:Name="webView1" Source="ms-appx-web:///HTMLPage1.html" Width="auto" Height="auto"/>

这是我的C#代码

webView1.ScriptNotify += webView1_ScriptNotify;
webView1.NavigationCompleted += webView1_NavigationCompleted;


async void webView1_NavigationCompleted(WebView sender, WebViewNavigationCompletedEventArgs args)
{
      await webView1.InvokeScriptAsync("eval", new string[] { "window.confirm = function(confirmMessage) { window.external.notify('typeConfirm:' + confirmMessage) }" });
      await webView1.InvokeScriptAsync("eval", new string[] { "window.alert = function(AlertMessage) { window.external.notify('typeAlert:' + AlertMessage) }" });
}

 private async void webView1_ScriptNotify(object sender, NotifyEventArgs e)
 {
            Windows.UI.Popups.MessageDialog dialog;
            string[] messageArray = e.Value.Split(':');
            string message;
            string type;

            if (messageArray.Length > 1)
            {
                message = messageArray[1];
                type = messageArray[0];
            }
            else
            {
                message = e.Value;
                type = "typeAlert";
            }
            dialog = new Windows.UI.Popups.MessageDialog(message);
            Debug.WriteLine("type=" + type + " ,message=" + message);

            if (type.Equals("typeConfirm"))
            {
                dialog.Commands.Add(new UICommand(
                    "Yes",
                    new UICommandInvokedHandler(this.CommandInvokedHandler)));
                dialog.Commands.Add(new UICommand(
                    "Cancel",
                    new UICommandInvokedHandler(this.CommandInvokedHandler)));

                dialog.DefaultCommandIndex = 0;

                dialog.CancelCommandIndex = 1;
            }
            var result = await dialog.ShowAsync();
            if (result.Label.Equals("Yes"))
            {
               // return true; 
            }
            else
            {
              // return false
            }
 }

问题是,确认js函数将始终返回false

在用户单击“是”或“否”之前。

我可以让用户选择按钮。 但是为时已晚。

在这种情况下,js函数“确认”将永远不会返回true。

有人可以帮助我吗?

谢谢。

这是行不通的。 JS在单线程上运行,并且不会等待c#调用的结果。 因此,警报将不会等待MessageDialog的结果。

请注意,winrt项目中的webview更适合显示一些静态html。 如果您确实想弹出一个MessageDialog作为确认对话框,那么您需要完成所有以前用c#代码在javascript中所做的其余工作。

例如,如果要更改html控件的某些文本,则需要准备完整的html字符串,然后在CommandInvokedHandler回调中检查命令状态,并让webview导航到新的html字符串(webView.NavigateToString)。

如果您需要在从WebView托管脚本发送的应用代码中接收通知和数据,则需要处理ScriptNotify事件。 您可以参考此样本 (scenario6)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM