2016-03-08 24 views
0

以下示例在堆棧溢出中,我放在一起MessageDialog顯示我的用戶錯誤消息。 在模擬器中,它工作正常。Windows Phone 8.1模式對話框。怎麼樣?

在手機上,它直接吹過,在屏幕上閃爍MessageDialog一會兒,甚至吹過一個我作爲解決方法放入的Task.Delay。

請問有人請向我解釋發生了什麼事情,或指出我朝着正確的方向?

p.s.我也在這裏嘗試了一篇ContentDialog。這甚至不顯示消息文本。

這裏有一個代碼片段:

public static async void ShowAndGo (String MessCode, String MessText, Boolean Xit) 
{ 
    String Mess = "";        // Start out with an empty Message to tell Joe User. 
    String Title = "";        // And an empty title too. 

    if (MessCode != "")        // If we're sent a Message "Code," 
     Mess = App.ResLdr.GetString (MessCode) + Cx.ld + Cx.ld; // turn it into text, culturally-aware. 
    Mess += MessText;        // Stick MessText onto the end of it. 

    if (Xit) 
     Title = App.ResLdr.GetString ("OhSnap"); // If we're goin' down, curse a little. 
    else 
     Title = App.ResLdr.GetString ("NoProb"); // If it's just informational, no problem-o. 

    MessageDialog messageDialog = new MessageDialog (Mess, Title); 
    await messageDialog.ShowAsync();    // IT FREAKING ISN'T STOPPING HERE!!! 
    Task.Delay (10000).Wait();      // Wait 10 seconds with error message on the screen. 
                // AND IT FREAKING DOESN'T STOP HERE EITHER!!! 
} 

回答

1

你的問題的原因很簡單 - 您聲明異步無效方法 - 避免這種情況,這應該只在特殊情況下,例如事件。在你的代碼,你的程序就行不會停止在那裏你調用方法:

ShowAndGo("Message code", "Message Text", false); 
Debug.WriteLine("Something happening"); 

它可能表現的消息,但多久它就會生存下來就看你的代碼進一步。造成這種情況的補救方法是從無效任務等待改變方法:

public static async Task ShowAndGo (String MessCode, String MessText, Boolean Xit) 
{ /* method */ } 

//invoke: 
await ShowAndGo("Message code", "Message Text", false); 
Debug.WriteLine("Something happening"); // now it should wait till user clicks OK 

當然,這需要異步一路,但可能這是你的程序應該是什麼樣子等。