我正在使用Mahapp,正在尝试等待对话框的结果,但是编译器在ShowMessageAsync下划线并显示给我:


  ShowMessageAsync在当前上下文中不存在


这是代码:

private async void ShowMessageBox(object sender, RoutedEventArgs e)
{
    var result = await ShowMessageAsync("Hello!", "Welcome to the world of metro!",
        MahApps.Metro.Controls.MessageDialogStyle.AffirmativeAndNegative);
    if (result == MessageDialogResult.Affirmative)
    {
        this.ShowMessageAsync("Result", "You said: OK");
    }
    else
    {
        this.ShowMessageAsync("Result", "You said: CANCEL");
    }
}

最佳答案

mahapps异步消息框的扩展方法。

using System.Windows;
using MahApps.Metro.Controls;
using MahApps.Metro.Controls.Dialogs;
using System.Threading.Tasks;
public static class InfoBox
{
    public async static Task<MessageDialogResult> ShowMessageAsync(string title, string Message, MessageDialogStyle style = MessageDialogStyle.Affirmative, MetroDialogSettings settings = null)
    {
         return await ((MetroWindow)(Application.Current.MainWindow)).ShowMessageAsync(title, Message, style, settings);
    }
}


用法

var res = await InfoBox.ShowMessageAsync(...);
if (res == MessageDialogResult.Affirmative)
{
     /* Do something*/
}

10-01 16:21