Using a SynchronizationContext
In all Xamarin.Forms
applications, when we update view-model properties that are bound to a view, they must be changed on the main UI thread.
Tip
This rule applies to any application. UI changes must happen on the main UI thread.
The SynchronizationContext.Current
property is used to retrieve the current sync context of any thread.
How do we know this context is from the main UI thread?
We store a reference to this context in the constructor because all view-models are created on the main UI thread. This means we have the current sync context of the main thread.
Let's have a look at how we are going to use this sync context reference:
#region Private Methods private void UpdateFiles() { _context.Post(async (obj) => { Cells.Clear(); var files = await _storage.GetTable<FileStorable>(CancellationToken.None); foreach (var file in files) { var fileModel = _fileFactory(); fileModel...