0

按照 Stack Overflow 中的示例,我整理了一个 MessageDialog 来显示我的用户错误消息。在模拟器中,它工作正常。

在电话上,它直接通过,仅在屏幕上闪烁 MessageDialog 片刻,甚至通过 Task.Delay 我作为一种解决方法输入。

有人可以向我解释发生了什么,或者指出我正确的方向吗?

ps 我还在这里为每篇文章尝试了一个 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!!!
}
4

1 回答 1

1

您的问题的原因很简单 - 您正在声明async void方法 - 避免这种情况,这应该只在特殊情况下使用,例如事件。在您拥有的代码中,您的程序不会在您调用该方法的地方停止:

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

它可能会显示一条消息,但它会存活多长时间取决于您的进一步代码。对此的补救措施是将方法从void更改为Taskawait

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

当然这需要一直 async,但可能这就是你的程序应该的样子。

于 2016-03-08T06:05:05.430 回答