arachide
arachide

Reputation: 8066

popup a message dialog with return value

I am a newbie of c# & uwp

my code to popup a message dialog is

    private void messageBoxClick(IUICommand command)
    {
        // Display message showing the label of the command that was invoked
        //rootPage.NotifyUser("The '" + command.Label + "' command has been selected.", NotifyType.StatusMessage);
    }

    public async Task messageBoxShow(string s, string commandString1, string commandString2)
    {
        var dialog = new Windows.UI.Popups.MessageDialog(s);
        dialog.Commands.Add(new UICommand(commandString1, new UICommandInvokedHandler(this.messageBoxClick)));
        dialog.Commands.Add(new UICommand(commandString2, new UICommandInvokedHandler(this.messageBoxClick)));

        await dialog.ShowAsync();


    }

it works! but the style I hope to get is

    string s = messageBoxShow(s, commandString1, commandString2);

Is it possible to change the former style to this one

Your comment welcome

Upvotes: 0

Views: 511

Answers (1)

Anran Zhang
Anran Zhang

Reputation: 7727

The display of MessageDialog is an asynchronous operation, and the result returned by ShowAsync is IUICommand. If the string value you want to get is IUICommand.Label, you can write like this:

public async Task<string> messageBoxShow(string s, string commandString1, string commandString2)
{
    var dialog = new MessageDialog(s);
    dialog.Commands.Add(new UICommand(commandString1, new UICommandInvokedHandler(this.messageBoxClick)));
    dialog.Commands.Add(new UICommand(commandString2, new UICommandInvokedHandler(this.messageBoxClick)));
    var result = await dialog.ShowAsync();
    return result.Label;
}

Usage

string label = await messageBoxShow(s, commandString1, commandString2);

Upvotes: 3

Related Questions