Now that we have a service that's responsible for fetching weather data from the external weather source, it's time to create a ViewModel. First, however, we will create a base view model where we can put the code that can be shared between all the ViewModels of the app. Let's set this up:
- Create a new folder called ViewModels.
- Create a new class called ViewModel.
- Make the new class public and abstract.
- Add and implement the INotifiedPropertyChanged interface. This is necessary because we want to use data bindings.
- Add a Set method. This will make it easier to raise the PropertyChanged event from the INotifiedPropertyChanged interface. The method will check whether the value has changed. If it has, it will raise the event:
public abstract class ViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void Set<T>(ref T field, T newValue,
[CallerMemberName...