首页 > 解决方案 > 弹出带有返回值的消息对话框

问题描述

我是 c# & uwp 的新手

我弹出消息对话框的代码是

    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();


    }

有用!但我希望得到的风格是

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

是否可以将以前的样式更改为这个样式

欢迎您的评论

标签: c#uwp

解决方案


的显示MessageDialog是异步操作,返回的结果ShowAsyncIUICommand。如果要获取的字符串值为IUICommand.Label,可以这样写:

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

用法

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

推荐阅读