The #39;await#39; operator can only be used within an async lambda expression(await 运算符只能在异步 lambda 表达式中使用)
问题描述
我有一个 c# Windows Store 应用程序.当单击另一个 MessageDialog
内的命令按钮之一时,我正在尝试启动 MessageDialog
.这样做的目的是警告用户他们的内容未保存,如果他们单击取消,它将提示他们使用单独的保存对话框进行保存.
I've got a c# Windows Store app. I'm trying to launch a MessageDialog
when one of the command buttons inside another MessageDialog
is clicked. The point of this is to warn the user that their content is unsaved, and if they click cancel, it will prompt them to save using a separate save dialog.
这是我的showCloseDialog"函数:
Here's my "showCloseDialog" function:
private async Task showCloseDialog()
{
if (b_editedSinceSave)
{
var messageDialog = new MessageDialog("Unsaved work detected. Close anyway?", "Confirmation Message");
messageDialog.Commands.Add(new UICommand("Yes", (command) =>
{
// close document
editor.Document.SetText(TextSetOptions.None, "");
}));
messageDialog.Commands.Add(new UICommand("No", (command) =>
{
// save document
await showSaveDialog();
}));
messageDialog.DefaultCommandIndex = 1;
await messageDialog.ShowAsync();
}
}
在 VS 中我得到一个编译器错误:
In VS I get a compiler error:
await"运算符只能在异步 lambda 表达式中使用.考虑用'async'修饰符标记这个lambda表达式`
The 'await' operator can only be used within an async lambda expression. Consider marking this lambda expression with the 'async' modifier`
方法用 await
标记.如果我从 showSaveDialog 之前删除 await
,它会编译(并且可以工作),但我会收到警告说我真的应该使用 await
The method is marked with await
. If I remove await
from before showSaveDialog, it compiles (and works) but I get a warning that I really should use await
如何在这种情况下使用 await
?
How do I use await
in this context?
推荐答案
你必须将你的 lambda 表达式标记为 async
,像这样:
You must mark your lambda expression as async
, like so:
messageDialog.Commands.Add(new UICommand("No", async (command) =>
{
await showSaveDialog();
}));
这篇关于'await' 运算符只能在异步 lambda 表达式中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:'await' 运算符只能在异步 lambda 表达式中使用


- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- 输入按键事件处理程序 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01