Windows Store app Development Snack: Async & Sharing

Here’s an interesting issue: you need to implement a Share Source but to do the sharing, it must be an async call. So what do you do? You can add the async and await modifiers, but it won’t work correctly. The solution is to use the deferral, which is provided in the arguments of the event, and when you’re done, you call the _Complete() method on it to indicate that you’re finished with all the async operations:

async void App_DataRequested(DataTransferManager sender, DataRequestedEventArgs args)
{
    var deferral = args.Request.GetDeferral();

    // async code with await keyword here

    deferral.Complete();
}