271

私はこのコードを持っています:

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の検査で、「この呼び出しは待機されていないため、呼び出しが完了する前に現在のメソッドの実行が続行されます。呼び出しの結果に「await」演算子を適用することを検討してください」(コメント)。

それで、私はそれに「await」を追加しましたが、もちろん、どこかに「async」を追加する必要があります-しかし、どこで?

4

3 に答える 3

423

ラムダ非同期をマークする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);
}));
于 2012-12-23T22:55:44.013 に答える
46

そして、匿名の表現を使用している人のために:

await Task.Run(async () =>
{
   SQLLiteUtils slu = new SQLiteUtils();
   await slu.DeleteGroupAsync(groupname);
});
于 2019-06-14T16:59:34.540 に答える
1

LINQメソッド構文内にいる場合は、パラメーターの直前にasyncキーワードを適用します。

 list.Select(async x =>
            {
                await SomeMethod(x);
                return true;
            });
于 2021-11-15T15:46:33.700 に答える