The ViewModel is the mediator between the View and the Model. Let's create a base class for ViewModels with common functionality that we can reuse. In practice, the ViewModel must implement an interface called INotifyPropertyChanged in order for MVVM to function. We will do so in the base class, and will also add a little handy helper tool called PropertyChanged.Fody that will save us a lot of time. Again, please check out Chapter 2, Building Our First Xamarin. Forms App, if you are feeling unsure about MVVM.
The first step is to create a base class. Proceed as follows:
- In the News project, create a folder called ViewModels.
- In the ViewModels folder, create a class called ViewModel.
- Add using statements to System.ComponentModel and change the existing class to look like the following code:
using System.ComponentModel;
namespace News.ViewModels
{
public abstract class ViewModel
{
}
}
Excellent! Let's implement...