2013-04-11 18 views
1

所以,我將應用程序移植到Windows Store。在應用程序開始時,我有一些代碼,它提出了一個問題。我不想讓我的代碼的其餘部分發作直到我得到答覆。Windows Store中的消息對話框沒有異步?

我有這樣的:

 string message = "Yadda Yadda Yadda"; 
     MessageDialog msgBox = new MessageDialog(message, "Debug Trial"); 
     msgBox.Commands.Add(new UICommand("OK", 
        (command) => { curSettings.IsTrial = true; })); 
     msgBox.Commands.Add(new UICommand("Cancel", 
        (command) => { curSettings.IsTrial = false; })); 
     await msgBox.ShowAsync(); 

     //... more code that needs the IsTrial value set BEFORE it can run... 

當我運行應用程序時,msgBox.ShowAsync後()的代碼運行時,沒有正確的值被設置。只有在方法結束後用戶才能看到對話框。

我想這樣做更像是一個提示,其中程序WAITS爲用戶點擊繼續之前的方法。我怎麼做?

+0

所以...什麼是你的問題? – 2013-04-11 00:49:03

回答

2

MessageDialog沒有用於「顯示」的非異步方法。如果您想在繼續之前等待對話框的響應,您可以簡單地使用await關鍵字。

這裏還有一個用於Windows應用商店中的異步編程的quickstart guide

我看到你的代碼示例已經使用「await」。您還必須將調用函數標記爲「異步」才能正常工作。

例子:

private async void Button1_Click(object sender, RoutedEventArgs e) 
{ 
    MessageDialog md = new MessageDialog("This is a MessageDialog", "Title"); 
    bool? result = null; 
    md.Commands.Add(
     new UICommand("OK", new UICommandInvokedHandler((cmd) => result = true))); 
    md.Commands.Add(
     new UICommand("Cancel", new UICommandInvokedHandler((cmd) => result = false))); 

    await md.ShowAsync(); 

    if (result == true) 
    { 
     // do something 
    } 
} 
+0

也許這就是困惑所在。它採用異步方式,但不會等待。 – 2013-04-17 16:22:00

+0

您需要發佈完整的代碼示例,而不僅僅是修剪片段。 – BTownTKD 2013-04-17 18:01:24