B. *_*non 199 c# resharper lambda async-await windows-store-apps
我有这个代码:
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));
}
Run Code Online (Sandbox Code Playgroud)
...... Resharper的检查抱怨说," 因为没有等待这个呼叫,所以当呼叫完成之前,当前方法的执行仍在继续.考虑将'await'运算符应用于呼叫结果 "(在线路上评论).
所以,我预先"等待"它,但当然,我需要在某处添加"异步" - 但在哪里?
Bol*_*ock 339
要标记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);
}));
Run Code Online (Sandbox Code Playgroud)
Su *_*lyn 16
对于使用匿名表达式的人:
await Task.Run(async () =>
{
SQLLiteUtils slu = new SQLiteUtils();
await slu.DeleteGroupAsync(groupname);
});
Run Code Online (Sandbox Code Playgroud)
小智 10
如果您使用 LINQ 方法语法,请在参数之前应用 async 关键字:
list.Select(async x =>
{
await SomeMethod(x);
return true;
});
Run Code Online (Sandbox Code Playgroud)