2012-12-23 12 views
146

ho questo codice:Dove posso contrassegnare un'espressione lambda async?

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

... che l'ispezione di ReSharper lamentato con "Perché questo invito non è atteso, l'esecuzione del metodo corrente continua prima che la chiamata è stata completata in considerazione l'applicazione del. 'attendere' l'operatore al risultato della chiamata "(sulla riga con il commento).

E così, ho anteposto una "attesa" ad esso, ma, naturalmente, ho poi bisogno di aggiungere un "async" da qualche parte, anche - ma dove?

risposta

236

Per contrassegnare un async lambda, è sufficiente anteporre async prima della sua lista degli argomenti:

// 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

così semplice ... ma non ovvio a tutti! +1 – ppumkin

Problemi correlati