如何标记异步lambda表达式?

时间:2018-09-18 10:04:55

标签: 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));
}

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

转售商检查抱怨:“由于未等待此调用,因此在调用完成之前将继续执行当前方法。请考虑将'await'运算符应用于调用结果”

非常感谢您的帮助!

3 个答案:

答案 0 :(得分: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);
}));

答案 1 :(得分:3)

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

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

答案 2 :(得分:3)

只需将其添加到括号前面,就像这样:

contextMenu.Commands.Add(new UICommand("Edit this Group", async (contextMenuCmd) =>
{
相关问题