gpt4 book ai didi

c# - 不能在 ShowMessageAsync 上使用 await

转载 作者:行者123 更新时间:2023-11-30 15:21:34 25 4
gpt4 key购买 nike

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

ShowMessageAsync doesn't exist in the current context

这是代码:

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*/
}

关于c# - 不能在 ShowMessageAsync 上使用 await,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37319540/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com