I really appreciate the combination of using CanExecute
, NotifyCanExecuteChangedFor
, and AsyncRelayCommand<T>.IsRunning
property to run/stop the ActivityIndicator and disable/enable the Command button without having to keep track of boolean properties allover the ViewModel, as described in this excellent blog post.
Yet, I’m left wondering, how about all other flags?
For example, the following code sample supports a XAML file that contains a single Picker Control (No buttons here). Before loading the Collection, the ActivityIndicator needs to run and the Picker has to be disabled, and after the collection is loaded, the flags are reversed. As the app gets complicated, other flags will have to be added.
Is there a better way out there to accomplish those tasks without relying on boolean flags?
public partial class TestViewModel: ObservableObject
{
[ObservableProperty]
private Book _selectedBook;
[ObservableProperty]
ObservableCollection<Book> _books;
[ObservableProperty]
bool _showIndicator;
[ObservableProperty]
bool _pickerEnabled;
public TestViewModel()
{
BooksService();
}
async Task BooksService()
{
ShowIndicator = true;
PickerEnabled = false; //by default
await Task.Delay(TimeSpan.FromSeconds(5));
Books = await FetchBooks();
ShowIndicator = false;
PickerEnabled = true;
}
}