我有这样的代码:
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));
}
...that Resharper的检查抱怨道,“由于此调用不是等待的,因此在调用完成之前会继续执行当前方法。请考虑将'await‘运算符应用于调用的结果”(在带有注释的行上)。
因此,我在它前面添加了一个"await“,但是,当然,我也需要在某个地方添加一个"async”--但是在哪里呢?
发布于 2012-12-24 06:55:44
要将lambda async标记为异步,只需在其参数列表前添加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);
}));
发布于 2019-06-15 00:59:35
对于那些使用匿名表达式的人:
await Task.Run(async () =>
{
SQLLiteUtils slu = new SQLiteUtils();
await slu.DeleteGroupAsync(groupname);
});
发布于 2021-11-15 15:46:34
如果在LINQ方法语法中,请在参数前应用async关键字:
list.Select(async x =>
{
await SomeMethod(x);
return true;
});
https://stackoverflow.com/questions/14015319
复制相似问题