简体   繁体   中英

Windows Phone 8.1 Modal Dialog. How?

Following examples here in Stack Overflow, I put together a MessageDialog to show my user error messages. In the emulator, it works fine.

On the phone, it blows right through, flashes the MessageDialog on the screen for only a moment, and even blows through a Task.Delay I put in as a workaround.

Would somebody please explain to me what's happening, or point me in the right direction?

ps I also tried a ContentDialog per an article here. THAT doesn't even display the message text.

Here's a code snippet:

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!!!
}

The reason of your problem is simple - you are declaring async void method - avoid that, this should be used only in special cases, for example events. In the code you have, your program doesn't stop on line where you invoke the method:

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

It probably shows a message, but how long it will survive it depends on your further code. The remedy for this is to change the method from void to Task and await :

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

Of course that requires async all the way , but probably that's how your program should look like.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM