我如何处理确认对话框在webview?UWP windows 10应用程序c#

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

本文关键字:UWP webview windows 应用程序 对话框 何处理 处理 确认      更新时间:2023-09-26

我正在用c#开发uwp应用程序(win10)

我想把我的网站在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函数"confirm"在这种情况下永远不会返回true。

有人能帮我吗?

谢谢。

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

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

例如,如果你想改变html控件的一些文本,你需要准备一个完整的html字符串,然后检查CommandInvokedHandler回调中的命令状态,并让webview导航到(webview . navigatetostring)新的html字符串

如果你需要在应用代码中接收来自webview托管脚本的通知和数据,你需要处理ScriptNotify事件。您可以参考这个示例(场景6)。