Boris Pluimvee
Boris Pluimvee

Reputation: 407

How do I popup in message in UWP?

In my Windows 8.1 app I used MessageBox.Show() to popup a message. That is gone in UWP. How can I show a message?

Upvotes: 8

Views: 13630

Answers (3)

AfamO
AfamO

Reputation: 899

It is better to put MessageDialog codes into a function that has a keyword async and returns type of Task For example:

public async Task displayMessageAsync(String title, String content,String dialogType)
    {
        var messageDialog = new MessageDialog(content, title);
        if (dialogType == "notification")
        {
            //Do nothing here.Display normal notification MessageDialog
        }
        else
        {
            //Dipplay questions-Yes or No- MessageDialog
            messageDialog.Commands.Add(new UICommand("Yes", null));
            messageDialog.Commands.Add(new UICommand("No", null));
            messageDialog.DefaultCommandIndex = 0;
        }

        messageDialog.CancelCommandIndex = 1;
        var cmdResult = await messageDialog.ShowAsync();
        if (cmdResult.Label == "Yes")
        {
            Debug.WriteLine("My Dialog answer label is:: " + cmdResult.Label);
        }
        else
        {
            Debug.WriteLine("My Dialog answer label is:: " + cmdResult.Label);
        }
    }

Upvotes: 0

Jessevl
Jessevl

Reputation: 919

Yup, indeed something like that, the new method is to use the MessageDialog class. You have to create an object of that type. You can also add buttons. It's a bit more complex I think. But you can also use some shortcuts here. To just show a message, use this:

await new MessageDialog("Your message here", "Title of the message dialog").ShowAsync();
To show an simple Yes/No message, you can do it like this:

MessageDialog dialog = new MessageDialog("Yes or no?");
dialog.Commands.Add(new UICommand("Yes", null));
dialog.Commands.Add(new UICommand("No", null));
dialog.DefaultCommandIndex = 0;
dialog.CancelCommandIndex = 1;
var cmd = await dialog.ShowAsync();

if (cmd.Label == "Yes")
{
    // do something
}

Upvotes: 15

Robin-Manuel Thiel
Robin-Manuel Thiel

Reputation: 2256

Take a look at the Windows.UI.Popups.MessageDialog class and try this:

// Create a MessageDialog
var dialog = new MessageDialog("This is my content", "Title");

// If you want to add custom buttons
dialog.Commands.Add(new UICommand("Click me!", delegate (IUICommand command)
{
    // Your command action here
}));

// Show dialog and save result
var result = await dialog.ShowAsync();

Upvotes: 9

Related Questions