2015-10-19 82 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

嗨方芳武 感謝您的回覆。 我學習這個樣本後。 它沒有提到如何獲得確認函數的返回值。 –

相關問題