2012-12-23 26 views
146

मैं इस कोड मिल गया है:मैं लैम्ब्डा अभिव्यक्ति 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)); 
} 

... Resharper का निरीक्षण शिकायत की कि के साथ, के बारे में "क्योंकि इस कॉल की प्रतीक्षा नहीं है, वर्तमान पद्धति के निष्पादन कॉल पूर्ण होने से पहले जारी है को लागू करने पर विचार करें। कॉल "(टिप्पणी के साथ लाइन पर) के परिणामस्वरूप ऑपरेटर 'प्रतीक्षा करें'।

और इसलिए, मैंने इसे "प्रतीक्षा" की तैयारी की, लेकिन निश्चित रूप से, मुझे फिर भी "async" जोड़ने की ज़रूरत है - लेकिन कहां?

उत्तर

236

एक लैम्ब्डा async चिह्नित करने के लिए, बस अपने तर्क सूची से पहले 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