2015-10-19 78 views
0

我正在开发uwp应用程序(赢得10)由c#如何在webview中处理确认对话框? UWP窗口10应用程序C#

我想把我的网站在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

用户点击yes或no之前。

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

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

任何人都可以帮助我吗?

谢谢。

回答

0

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

请注意,在winrt项目中的webview更适合显示一些静态HTML。如果你确实想弹出一个MessageDialog作为确认对话框,那么你需要完成你以前在c#代码中用javascript做的所有休息工作。例如,如果要更改html控件的某些文本,则需要准备一个完整的html字符串,然后检查CommandInvokedHandler回调中的命令状态,并让webview导航到(webView.NavigateToString)新的html字符串。

+0

嗨Alan Yao,谢谢你的回复。 我知道你的意思。 –

+0

在我的senario中,该网站已经建成了很长一段时间。所以我不知道有多少次确认函数调用。因此我无法一个接一个地处理CommandInvokedHandler。我需要确保原始网站功能正常工作。感谢您的回复。现在我知道这是不可能等待C#调用的结果。 –

0

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

+0

嗨方芳武 感谢您的回复。 我学习这个样本后。 它没有提到如何获得确认函数的返回值。 –

相关问题