Is there a well-defined pattern for binding controls to asynchronous data? Is there a well-defined pattern for binding controls to asynchronous data? wpf wpf

Is there a well-defined pattern for binding controls to asynchronous data?


As others have noted, the "asynchronous" members in WPF types have nothing to do with async and await.

You do have a problem in your binding; your path is using Task.Result, which will block the UI thread until the query is complete. Also, using Result brings up the possibility of a deadlock that I describe on my blog.

I have another blog entry that deals with async properties, in particular how to data-bind to properties that are (logically) asynchronous. My AsyncEx library has a type called NotifyTaskCompletion that allows you to data-bind more naturally to an asynchronous Task.

So, e.g., you could do something like this:

public class HottestPageProxy{  public HottestPageProxy()  {    Artists = NotifyTaskCompletion.Create(GetArtists());  }  public INotifyTaskCompletion<ArtistsQuery> Artists { get; private set; }  private Task<ArtistsQuery> GetArtists()  {    string apiKey = App.GetApiKey();    return Queries.ArtistTopHottt(new ArtistTopHotttParameters    {        ApiKey = apiKey,        Results = 100,        Buckets = new[] {ArtistTopHotttBuckets.Hotttnesss}    });  }}

Then you can data-bind to several different properties on INotifyTaskCompletion<T>, including IsCompleted, ErrorMessage, and Result (which does not block; it will return default(T) if the task is not completed).