2012-12-23 29 views
146

我有這樣的代碼:我在哪裏標記一個lambda表達式異步?

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的檢查抱怨被調用完成前大約用,「因爲這個電話是不是等待,當前方法的執行繼續考慮應用。 '等待'運營商致電的結果「(與評論一致)。

因此,我預先安排了一個「等待」,但當然,我還需要在某處添加「異步」 - 但是在哪裏?

回答

236

爲了紀念一個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); 
})); 
+0

那麼簡單......但不是很明顯了! +1 – ppumkin