不能在 ShowMessageAsync 上使用 await

Dil*_*ger 4 c# mahapps.metro

我正在使用 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");
    }
}
Run Code Online (Sandbox Code Playgroud)

Ami*_*san 5

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

用法

var res = await InfoBox.ShowMessageAsync(...); 
if (res == MessageDialogResult.Affirmative) 
{ 
     /* Do something*/ 
}
Run Code Online (Sandbox Code Playgroud)