Async operations in a XAML application
April 20th 2021Scheduling async tasks
It's very common for a XAML application (or really, any kind of application) to have to load a good amount of data on startup to populate its initial state. When doing so it is often important to avoid blocking the UI, for example while reading the filesystem or fetching data from an external API, as we have no idea how long those operations will take. For this reason we generally want data to be fetched from a worker thread, in an asynchronous fashion, while keeping the UI reactive to user actions.
It's of course not limited to the initial state, we want to be able to do asynchronous work almost every time the user clicks on a button or edit a text box.
In the .net world async work can be easily managed by using System.Threading.Tasks.Task
, a class representing asynchronous operations and for which C# has nice syntaxic sugar in the form of the (now quite common) async
/await
keywords.
The general pattern to do async in a XAML context is the fo...