Moving event handling to ViewModel
Suppose that when a Button is pressed, we want to handle that fact in the ViewModel
, as is preferred by the Model-View-ViewModel (MVVM) pattern. However, one of the things we want to do, in response to that Button press, is to show a ProgressBar
.
Handling things in ViewModel
gets tricky when we want to interact with the user interface (UI). However, it can be done in a number of ways. Let’s modify the Submit button, remove its Clicked
event and add a command:
<Button BackgroundColor="Gray" Command="{Binding SubmitCommand}" Margin="5" Text="Submit" />
We’ll create RelayCommand
in ViewModel
to handle the Submit
Command:
[RelayCommand] private async void Submit() { for (var i = 0.0; i < 1.0; i += 0.1) { await LoginPage.LoginProgressBar...