Creating the WalkBaseViewModel for the TrackMyWalks app

In this section, we will begin by creating a base MVVM ViewModel that will be used by each of our ViewModels when we create these, the Views (pages) will then implement those ViewModels and use them as their BindingContext.

Tip

We will start by creating a base ViewModel class that will essentially be an abstract class, containing basic functionality that each of our ViewModels will inherit from, and will implement the INotifyPropertyChanged Interface.

Let's take a look at how we can achieve this, by following these steps:

  1. Create an empty class within the ViewModels folder, shown in the following screenshot:

    Creating the WalkBaseViewModel for the TrackMyWalks app

  2. Next, choose the Empty Class option located within the General section, and enter in WalkBaseViewModel for the name of the new class file to be created, as shown in the following screenshot:

    Creating the WalkBaseViewModel for the TrackMyWalks app

  3. Next, click on the New button to allow the wizard to proceed and create the new empty class file, as shown in the preceding screenshot.

    Up until this point, all we have done is create our WalkBaseViewModel class file. This abstract class will act as the base ViewModel class that will contain the basic functionality that each of our ViewModels will inherit from.

    As we start to build the base class, you will see that it contains a couple of members and it will implement the INotifyPropertyChangedInterface. As we progress through this book, we will build to this class, which will be used by the TrackMyWalks application. To proceed with creating the base ViewModel class.

    Ensure that the WalkBaseViewModel.cs file is displayed within the code editor, and enter in the following code snippet:

            //
            // WalkBaseViewModel.cs
            // TrackMyWalks Base ViewModel
            //
            // Created by Steven F. Daniel on 22/08/2016.
            // Copyright © 2016 GENIESOFT STUDIOS. All rights reserved.
            //
            using System.ComponentModel;
            using System.Runtime.CompilerServices;
    
            namespace TrackMyWalks.ViewModels
            {
             public abstract class WalkBaseViewModel :
               INotifyPropertyChanged
             {
             protected WalkBaseViewModel()
             {
             }
    
             public event PropertyChangedEventHandler PropertyChanged;
    
             protected virtual void OnPropertyChanged
               ([CallerMemberName] string propertyName = null)
             {
             var handler = PropertyChanged;
             if (handler != null)
             {
             handler(this, new PropertyChangedEventArgs(propertyName));
             }
             }
             }
            }
     
    

In the preceding code snippet, we begin by creating a new abstract class for our WalkBaseViewModel that implements from the INotifyPropertyChanged interface class, which allows the View or page to be notified whenever properties contained within the ViewModel have changed. Next, we declare a variable PropertyChanged that inherits from the PropertyChanged EventHandler that will be used to indicate whenever properties on the object have changed. Finally, within the OnPropertyChanged method, this will be called when it has determined that a change has occurred on a property within the ViewModel from a child class.

Note

The INotifyPropertyChanged interface is used to notify clients, typically binding clients, when the value of a property has changed.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.11.227