【问题标题】:How can i handle confirm dialog in webview? UWP windows 10 app C#我如何处理 webview 中的确认对话框? UWP windows 10 应用程序 C#
【发布时间】:2016-01-17 07:23:51
【问题描述】:

我正在用 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
            }
 }

问题是confirm js函数总是返回false

在用户点击是或否之前。

我可以获得用户选择的按钮。但为时已晚。

js 函数 "confirm" 在这种情况下永远不会返回 true。

谁能帮帮我?

谢谢。

【问题讨论】:

    标签: javascript c# webview uwp


    【解决方案1】:

    这行不通。 JS在单线程上运行,不会等待c#调用的结果。所以警报不会等待 MessageDialog 的结果。

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

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

    【讨论】:

    • 嗨艾伦姚,感谢您的回复。我知道你的意思。
    • 在我的senario中,网站已经完全建立了很长时间。所以我不知道确认函数调用了多少次。结果我无法一一处理 CommandInvokedHandler 。我需要确保原始网站功能正常工作。感谢您的回复。现在我发现等待 c# 调用的结果是不可能的。
    【解决方案2】:

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

    【讨论】:

    • 您好吴方方感谢您的回复。在我研究了这个样本之后。它没有提到如何获取确认函数的返回值。
    猜你喜欢
    • 2011-02-13
    • 1970-01-01
    • 2015-11-08
    • 1970-01-01
    • 2016-08-02
    • 2016-11-20
    • 1970-01-01
    • 1970-01-01
    • 2020-01-17
    相关资源
    最近更新 更多