MVVM Lite For Data Binding In Xamarin.Form

Introduction

This article will demonstrate how to work with MVVM development in Xamarin.Forms application.

We are using MVVM light that will provide the facility to implement Model-View-ViewModel. This is a quick way for the development of an application and also provides a user the facility to customize and design the application.

Implementation

Open Visual Studio and select New Project.

Xamarin

Select project type and give the project a name.

Xamarin

Select the template as Blank App and code sharing as PCL.

Xamarin

Set the target and minimum platform versions as below.

Xamarin

Set the below configuration.

Xamarin

Open "Manage NuGet packages for Solutions" and install the following components.

Xamarin

The ViewModel directory will be added that contains two files MainViewModel.cs and ViewModelLocator.cs file, with a directory structure as below.

Xamarin

Open MainPage.xaml from portable project and modify the content as below.

  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"  
  3.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"  
  4.              xmlns:local="clr-namespace:MvvmDemo"  
  5.              x:Class="MvvmDemo.MainPage" Title="MVVMLightDemo" BackgroundColor="Cyan">  
  6.     <StackLayout Padding="60">  
  7.         <Label Text="Enter Data: "></Label>  
  8.         <Entry Text="{Binding MyData}"></Entry>  
  9.         <Label Text="Below is the binded data: "></Label>  
  10.         <Label Text="{Binding MyData}"></Label>  
  11.     </StackLayout>  
  12. </ContentPage>  

Open MainPage.xaml.cs and modify the content as below.

  1. using Xamarin.Forms;  
  2. using MvvmDemo.ViewModel;  
  3. namespace MvvmDemo  
  4. {  
  5.     public partial class MainPage : ContentPage  
  6.     {  
  7.         MainViewModel mvm;  
  8.         public MainPage()  
  9.         {  
  10.             InitializeComponent();  
  11.             mvm = new MainViewModel();  
  12.             BindingContext = mvm;  
  13.         }  
  14.     }  
  15. }  

Open MainViewModel.cs under ViewModel folder from portable project and modify the content as below.

  1. using GalaSoft.MvvmLight;  
  2.   
  3. namespace MvvmDemo1.ViewModel  
  4. {  
  5.     public class MainViewModel : ViewModelBase  
  6.     {  
  7.         private string _myData;  
  8.         public string MyData  
  9.         {  
  10.             get {  
  11.                 return _myData;  
  12.             }  
  13.             set {  
  14.                 this.Set(ref this._myData, value, "MyData");  
  15.             }  
  16.         }  
  17.         /// <summary>  
  18.         /// Initializes a new instance of the MainViewModel class.  
  19.         /// </summary>  
  20.         public MainViewModel()  
  21.         {  
  22.             this.MyData = "sample 2 way binding!";  
  23.         }  
  24.     }  
  25. }  

Note - Create a new project;  I have attached only portable project in this article.

Run the application now. You will get the following output.

Xamarin

You can see the bound value on the below label “sample 2-way binding!”.

Delete the Entry content.

Xamarin

The label that binds the entry value is also deleted. Now, give the new value to the entry box.

Xamarin

You can see that the same value binds the label. Any modification will immediate bind to the bound property. You can check the same output on Android Emulator.

Up Next
    Ebook Download
    View all
    Learn
    View all