2012-12-23 5 views
146

Ich habe diesen Code bekam:Wo markiere ich einen Lambda-Ausdruck 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)); 
} 

... dass ReSharper die Inspektion beschwerten sich über mit "Da dieser Anruf nicht erwartet wird, wird die Ausführung der aktuellen Methode weiter, bevor der Anruf beendet ist Betrachten Sie die Anwendung. 'Warten' Operator auf das Ergebnis des Anrufs "(in der Zeile mit dem Kommentar).

Und so, ich habe ein "warten" darauf, aber natürlich muss ich dann ein "async" irgendwo hinzufügen - aber wo?

Antwort

236

Um eine Lambda-Asynchron zu markieren, prepend einfach async vor der Argumentliste:

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

So einfach ... noch gar nicht auf der Hand! +1 – ppumkin

Verwandte Themen