How to Load Data Asynchronously in WPF UI using MVVM Data Binding?

 Most of the content which is displayed by the WPF Application is fetched from either the database or some Web service. 

Why do we need to load Data Asynchronously?

We might have seen that when we wait for the content to be available it tends to freeze our WPF Application/ Main Thread. Therefore to avoid that we need to fetch large content/Data in an asynchronous fashion from the Database or Remote Server.

But the challenge there is how do we update the UI asynchronously. It is also desirable to show the loading progress to the user using some spinner control so that user gets the feeling that content is loading. 

We know that if we query the Web service or the Windows Service for any result it returns a Task which encapsulates the result if we are using Asynchronous version of the API. We will not be able to do direct data binding with the task which is being returned as WPF does not supports it. 

Evaluating Different ways to load data in an Async Fashion

One way is to use the await to get the result of the task, which works but using a async void method is not preferred because error handling will not be taken care as there is no Task which is being returned. Following example demonstrates this case:


Async Void method


Here in the Initialize method we are trying to fetch the result from the windows service and update the property which is bound with the View. Normally if there is some exception thrown in async method, returned task will contain that information. Since this is an async void method if there is some exception thrown in this function the caller of the function will not be able to handle this as there is no Task context returned.

Preferred way is to Encapsulate the Task returned by the asynchronous function into an object which can be bound to the View as shown in the figure below:

Sample Code


View Model with Notify Task Completion


As shown in the code we are encapsulating the task retuned by the asynchronous API into an object of type NotifyTaskCompletion. In simple Terms it is a wrapper over the Task and exposes the properties of the task. NotifyTaskCompletion source code can be found in the msdn document : NotifyTaskCompletion

ProcessingTask object which is of type NotifyTaskCompletion can be bound to the WPF View class as shown below:

WPF View class with Task Binding

As shown in the xaml file above the ResultTxt TextBox Text is bound to the Result property of the ProcessingTask Object which is of type NotifyTaskCompletion. This can be seen as an asynchronous data binding where the text will be updated as soon as the task gets completed. 

Another advantage of using this type of pattern is that we will be able to show the user loading progress by using the IsNotCompleted property of the NotifyTaskCompletion. Whenever this property is true we can set the visibility of the Spinner Control to true.

Using this we will not block the Main WPF Application thread and the application will not freeze trying to load the large content. Below Screenshot shows the output where the Result is updated by the service:

Asynchronous Data Binding











 

 

Comments

Popular posts from this blog

Creating RESTful Minimal WebAPI in .Net 6 in an Easy Manner! | FastEndpoints

Mastering Concurrency with Latches and Barriers in C++20: A Practical Guide for Students

Graph Visualization using MSAGL with Examples