如何标记异步lambda表达式?

Ire*_*lia 3 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));
}
Run Code Online (Sandbox Code Playgroud)

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

Resharpers检查抱怨:"因为没有等待这个呼叫,所以在呼叫完成之前继续执行当前方法.考虑将'await'运算符应用于呼叫结果"

任何帮助是极大的赞赏!

foy*_*yss 8

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

  • 让我们保持交叉,"UICommand"期望一个异步委托.否则它可能[肆虐](https://blogs.msdn.microsoft.com/pfxteam/2012/02/08/potential-pitfalls-to-avoid-when-passing-around-async-lambdas/). (2认同)