2016-03-08 60 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 

当然,这需要异步一路,但可能这是你的程序应该是什么样子等。