Supporting actions with commands
Let's go back to the starting point of our journey into the MVVM pattern: a page where users can fill in their names and surnames. By adding properties and implementing the INotifyPropertyChanged
interface in the ViewModel, we have been able to remove the tight connection between the UI layer and code-behind class when it comes to storing and retrieving the data. But what about the action? Our previous example has a Button
control with an event handler, which includes the code to save the data. However, this is another XAML scenario that creates a deep connection between the code-behind class and the UI layer: an event handler can only be declared in the code-behind. We can't declare it in our ViewModel, where the data we need (the name and surname) is actually stored.
It's time to introduce commands, which are a way to express actions not with an event handler, but through a regular property. This scenario is enabled by a built-in...