如何标记异步lambda表达式?

问题描述 投票:3回答:3

我在这里有一些代码,并希望知道在哪里等待。我尝试过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'运算符应用于呼叫结果”

任何帮助是极大的赞赏!

c# lambda async-await
3个回答
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);
}));

3
投票

为了标记lambda异步,请使用以下语法:

async (contextMenuCmd) =>
{
   SQLiteUtils rfd = new SQLiteUtils();
   await rfd.DeleteGroupAsync(nameOfGroup ); 
}

3
投票

只需将其添加到括号前面,如下所示:

contextMenu.Commands.Add(new UICommand("Edit this Group", async (contextMenuCmd) =>
{
© www.soinside.com 2019 - 2024. All rights reserved.