c# - 如何标记异步 lambda 表达式?

标签 c# lambda async-await

我这里有一些代码,想知道在哪里放置等待。我已经尝试过 lamba => 和常规方法,但都没有成功。

private async void ContextMenuAbroad(object sender, RightTappedRoutedEventArgs args)
{
    CheckBox ckbx = null;
    if (sender is CheckBox)
    {
        ckbx = sender as CheckBox;
    }
    if (null == ckbx)
    {
        return;
    }
    string nameOfGroup = ckbx.Content.ToString();

    var contextMenu = new PopupMenu();

    contextMenu.Commands.Add(new UICommand("Edit this Group", (contextMenuCmd) =>
    {
        Frame.Navigate(typeof(LocationGroupCreator), nameOfGroup );
    }));

    contextMenu.Commands.Add(new UICommand("Delete this Group", (contextMenuCmd) =>
    {
        SQLiteUtils rfd = new SQLiteUtils();
        rfd.DeleteGroupAsync(nameOfGroup ); 
    }));

    await contextMenu.ShowAsync(args.GetPosition(this));
}

我添加了一个等待,但是我需要在某处添加一个异步......但是在哪里?

Resharpers 检查提示:“因为没有等待这个调用,当前方法的执行在调用完成之前继续。考虑将 'await' 运算符应用于调用的结果”

非常感谢任何帮助!

最佳答案

简单地在参数列表之前添加async

// Command to delete the current Group
contextMenu.Commands.Add(new UICommand("Delete this Group", async (contextMenuCmd) =>
{
    SQLiteUtils rfd = new SQLiteUtils();
    await rfd.DeleteGroupAsync(groupName);
}));

关于c# - 如何标记异步 lambda 表达式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52384065/

相关文章:

c# - 如果字符串在 C# 中包含 2 个句点

c# - 将类转换为动态并添加属性

c# - 访问修改后的闭包,这是 ReSharper 错误吗?

python - map() 中带有 if 条件的 Lambda

c# - 确保调用函数两次时返回 C# 异步任务的最佳方法是什么?

c# - async/await 对 IO 操作做了什么?

javascript - 使用 async/await 和 try/catch 进行分层 api 调用

c# - 如何获取任何文件/文件夹的系统图标

时间:2019-03-17 标签:c#transactionscopewithyield

java - 如何在 java 8 lambda 表达式中获取方法参数名称?