我在哪里标志着一个lambda表达式异步?(Where do I mark a lambda exp

2019-07-17 13:06发布

我有这样的代码:

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

    var contextMenu = new PopupMenu();

    // Add a command to edit the current Group
    contextMenu.Commands.Add(new UICommand("Edit this Group", (contextMenuCmd) =>
    {
        Frame.Navigate(typeof(LocationGroupCreator), groupName);
    }));

    // Add a command to delete the current Group
    contextMenu.Commands.Add(new UICommand("Delete this Group", (contextMenuCmd) =>
    {
        SQLiteUtils slu = new SQLiteUtils();
        slu.DeleteGroupAsync(groupName); // this line raises Resharper's hackles, but appending await raises err msg. Where should the "async" be?
    }));

    // Show the context menu at the position the image was right-clicked
    await contextMenu.ShowAsync(args.GetPosition(this));
}

......那ReSharper的检查抱怨着,“ 是在调用完成前因为这个呼叫没有等待,目前的方法会继续执行。考虑应用‘等待’操作的调用的结果 ”(与该行评论)。

所以,我的前缀“等待”来了,但是,当然,我则需要添加“异步”的地方,太 - 但在哪里?

Answer 1:

为了纪念一个lambda异步,简单地将async参数列表之前:

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


文章来源: Where do I mark a lambda expression async?