MVVM Databinding In Xamarin.Forms Using Fresh MVVM

Xamarin
Introduction

In this tutorial, we will learn how to perform the MVVM approach in Xamarin.Forms using FreshMVVM. MVVM is the best approach for Xamarin.Forms and WPF Applications. There are a lot of MVVM plugins or libraries like FreshMVVM, MVVMCross, Prism, etc. available to simplify MVVM implementations.

FreshMVVM

FreshMVVM is designed to perform MVVM easily and simply with the Xamarin.Forms application. It was created Michael Ridland. It has certain rules to perform MVVM Databinding. You can find the plugin from GitHub and NuGet.

Rules for FreshMVVM

The rules for MVVM are simple.

  • The Views (Pages) name should end with Page.
  • The namespace of both, Page and Pagemodel, should be same.
  • You don’t need to set BindingContext. The plugin will detect the View and ViewModel with its name.

Let us start coding with FreshMVVM.

Coding Part

Steps

I have split this part into 3 steps -

Step 1

Creating new Xamarin.Forms Projects.

Step 2

Setting up the plugin for Xamarin.Forms Application.

Step 3

Implementing Fresh MVVM.

Step 1

Create a new project by selecting New >> Project >> Xamarin Cross-Platform App and click OK.

Xamarin

Then, select Android and iOS platforms as shown below with Code Sharing Strategy as PCL or .NET Standard, and click OK.

Xamarin

Step 2

We will start coding for FreshMVVM. Create a new Xamarin.Forms Project. Open NuGet Package Manager against the solution and search for FreshMVVM Plug-in or paste the following NuGet Installation.

Install-Package FreshMvvm -Version 2.2.3

Click "Install" to install this plug-in against your PCL Project or .NET Standard Project.

Xamarin

Step 3

Create your XAML page (View) with name ending up with “Page”.

Xamarin

Create PageModel by creating a class name ending with “PageModel” and inherited with “FreshBasePageModel”,  as shown in the below screenshot.

Xamarin

The namespace of the Page and PageModel should be same as shown in the above screenshots.

  • The Binding Properties and Command Properties are implemented same as the normal MVVM approach.
  • To indicate the binding properties, we just changed the RaisePropertyChanged instead of OnPropertyChanged event in Normal MVVM.
  • The following code is used to raise the property changed.

    RaisePropertyChanged("MainPageLabel");

Set MainPage

To initialize the FreshMVVM Navigation, you should set the MainPage with FreshMVVM Navigation Container with the following code.

  • Open xaml.cs or App.cs and set MainPage.
    1. // To set MainPage for the Application  
    2. var page = FreshPageModelResolver.ResolvePageModel < MainPageModel > ();  
    3. var basicNavContainer = new FreshNavigationContainer(page);  
    4. MainPage = basicNavContainer;  

Navigation between Pages

FreshMVVM itself has navigation methods to make navigation between the pages.

  1. Use PushPageModel for pushing the page in the navigation stack or goto next page instead of PushAsync in normal MVVM.
    1. await CoreMethods.PushPageModel<SecondPageModel>();  
    It is equivalent to the following.
    1. Navigation.PushAsync(new SecondPage());  
  1. Use PopPageModel for popping the page from navigation stack instead of PopAsync in normal MVVM.
    1. await CoreMethods.PopPageModel();  
    It is equivalent to the following.
    1. Navigation.PopAsync();  
  1. Use PopToRoot to navigate from any page to root page instead of PopToAsync in normal MVVM.
    1. await CoreMethods.PopToRoot(animate:false);  
    It is equivalent to the following.
    1. Navigation.PopToRootAsync();  

Full Code for MainPageModel

You can find the code for MainPageModel below.

  1. using System.Collections.Generic;  
  2. using System.Linq;  
  3. using System.Text;  
  4. using System.Threading.Tasks;  
  5. using Xamarin.Forms;  
  6. using FreshMvvm;  
  7. using System;  
  8.   
  9. namespace FreshMVVMSample  
  10. {  
  11.     public class MainPageModel : FreshBasePageModel  
  12.     {  
  13.  
  14.         #region Default Override functions  
  15.         public override void Init(object initData)  
  16.         {  
  17.             base.Init(initData);  
  18.             MainPageLabel = "Welcome to Fresh Mvvm Tutorial!";  
  19.         }  
  20.   
  21.         public override void ReverseInit(object returnedData)  
  22.         {  
  23.             base.ReverseInit(returnedData);  
  24.         }  
  25.   
  26.         protected override void ViewIsAppearing(object sender, EventArgs e)  
  27.         {  
  28.             base.ViewIsAppearing(sender, e);  
  29.         }  
  30.   
  31.         protected override void ViewIsDisappearing(object sender, EventArgs e)  
  32.         {  
  33.             base.ViewIsDisappearing(sender, e);  
  34.         }  
  35.         #endregion  
  36.  
  37.         #region Commands  
  38.         public Command GotoSecondPageCommand  
  39.         {  
  40.             get  
  41.             {  
  42.                 return new Command(async () =>  
  43.                 {  
  44.                     await CoreMethods.PushPageModel<SecondPageModel>();  
  45.                 });  
  46.             }  
  47.         }  
  48.         #endregion  
  49.  
  50.         #region Properties  
  51.         string _mainPageLabel = string.Empty;  
  52.         public string MainPageLabel  
  53.         {  
  54.             get  
  55.             {  
  56.                 return _mainPageLabel;  
  57.             }  
  58.             set  
  59.             {  
  60.                 if (_mainPageLabel != value)  
  61.                 {  
  62.                     _mainPageLabel = value;  
  63.                     RaisePropertyChanged(nameof(MainPageLabel));  
  64.                 }  
  65.             }  
  66.         }  
  67.         #endregion  
  68.   
  69.     }  
  70. }  

Download Code

You can download the full source code from the top of the article.

X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now