2012-12-23 26 views
146

Tôi đã có mã này:Tôi đánh dấu biểu thức lambda ở đâu?

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)); 
} 

... đó kiểm tra Resharper của phàn nàn về với "Bởi vì cuộc gọi này không được chờ đợi, thực hiện các phương pháp hiện nay vẫn tiếp tục trước khi cuộc gọi được hoàn thành xem xét áp dụng. nhà điều hành 'đang chờ' đến kết quả của cuộc gọi "(trên dòng có nhận xét).

Và vì vậy, tôi đã thêm vào một "đang chờ đợi" cho nó nhưng, tất nhiên, sau đó tôi cần phải thêm "không đồng bộ" ở đâu đó, quá - nhưng ở đâu?

Trả lời

236

Để đánh dấu một async lambda, chỉ cần thêm vào trước async trước khi danh sách đối số của nó:

// 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); 
})); 
+0

Vì vậy, đơn giản ... nhưng không rõ ràng ở tất cả! +1 – ppumkin