繁体   English   中英

如何标记异步lambda表达式?

[英]How do I mark a async lambda expression?

我在这里有一些代码,并希望知道在哪里等待。 我尝试过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);
}));

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM