I am trying to write an MVVM screen for a WPF application using async keywords and expectations for writing asynchronous methods for 1. Initially loading the data, 2. Updating the data, 3. Saving the changes, and then updating. Although this works for me, the code is very dirty, and I cannot help but think that there should be a better implementation. Can anyone advise a simpler implementation?
This is a shortened version of my ViewModel:
public class ScenariosViewModel : BindableBase { public ScenariosViewModel() { SaveCommand = new DelegateCommand(async () => await SaveAsync()); RefreshCommand = new DelegateCommand(async () => await LoadDataAsync()); } public async Task LoadDataAsync() { IsLoading = true;
IsLoading is displayed in the view where it is attached to the busy indicator.
LoadDataAsync is called by the navigation framework when you first view the screen or when you click the refresh button. This method should synchronously set IsLoading, and then return control to the UI thread until the service returns data. Finally, throwing any exceptions so that they can be caught by the global exception handler (do not discuss!).
SaveAync is called by the button, passing the updated values ββfrom the form to the service. It must synchronously install IsLoading, call the Save method on the service asynchronously, and then start the update.
source share