我正在使用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");
}
}
答案 0 :(得分:4)
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*/
}
答案 1 :(得分:0)
您必须添加this
关键字,因为ShowMessageAsync
是一种扩展方法,而不是MetroWindow
类的成员。
var result = await this.ShowMessageAsync("Hello!", ...);
//^^^^^ here
您还有其他错误。而不是:
MahApps.Metro.Controls.MessageDialogStyle.AffirmativeAndNegative
使用:
MahApps.Metro.Controls.Dialogs.MessageDialogStyle.AffirmativeAndNegative
你必须在这些行之前添加等待:
if (result == MessageDialogResult.Affirmative)
{
await this.ShowMessageAsync("Result", "You said: OK");
//^^^^ here
}
else
{
await this.ShowMessageAsync("Result", "You said: CANCEL");
//^^^^ here
}