M Kenyon II
M Kenyon II

Reputation: 4264

Message Dialog in Windows Store WITHOUT Async?

So, I'm porting an app over to Windows Store. At the start of the app, I have some code, that asks a question. I DO NOT WANT THE REST OF MY CODE TO FIRE UNTIL I GET A RESPONSE.

I have this:

        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...

When I run the app, the code after the msgBox.ShowAsync() runs, without the correct value being set. It's only after the method finishes that the user sees the Dialog box.

I would like this to work more like a prompt, where the program WAITS for the user to click BEFORE continuing the method. How do I do that?

Upvotes: 1

Views: 1530

Answers (1)

Breealzibub
Breealzibub

Reputation: 8095

MessageDialog does not have a non-asynchronous method for "Show." If you want to wait for the response from the dialog before proceeding, you can simply use the await keyword.

Here also is a quickstart guide for asynchronous programming in Windows Store Apps.

I see that your code sample already uses "await". You must also mark the calling function as "async" in order for it to work properly.

Example:

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

Upvotes: 2

Related Questions