Two-Way Binding Or MVVM In Xamarin.Forms Application

Introduction

In this article, we are going to learn how to apply two-way binding in Xamarin forms application with the help of InotifyPropertyChanged System component model.

Use of InotifyPropertyChanged

It's an interface which is used for notifying the clients and typically binding the clients that the value of the property has been changed.

Implementation

Open Visual Studio and select New Project.

Xamarin

Select "Cross Platform App" option and give the project a name.

Xamarin

Select Blank App template and PCL from code sharing.

Xamarin

Set the target and minimum platform version.

Xamarin

Create a 'Models' folder inside MvvmDemo(Portable) project and add the following model file.

Add EmployeeModel.cs

  1. namespace MvvmDemo.Models {  
  2.     public class EmployeeModel {  
  3.         public string Name {  
  4.             get;  
  5.             set;  
  6.         }  
  7.         public string Designation {  
  8.             get;  
  9.             set;  
  10.         }  
  11.         public string Qualification {  
  12.             get;  
  13.             set;  
  14.         }  
  15.         public string Details {  
  16.             get;  
  17.             set;  
  18.         }  
  19.     }  
  20. }  

Create 'ViewModels' folder inside MvvmDemo(Portable) project and add the following file.

Add EmployeeViewModel.cs 

  1. using MvvmDemo.Models;  
  2. using System.ComponentModel;  
  3. using System.Runtime.CompilerServices;  
  4. using Xamarin.Forms;  
  5. namespace MvvmDemo.ViewModels {  
  6.     public class EmployeeViewModel: INotifyPropertyChanged {  
  7.         private EmployeeModel _employeeModel;  
  8.         public EmployeeModel EmployeeModel {  
  9.             get {  
  10.                 return _employeeModel;  
  11.             }  
  12.             set {  
  13.                 _employeeModel = value;  
  14.                 OnPropertyChanged();  
  15.             }  
  16.         }  
  17.         public EmployeeViewModel() {  
  18.             EmployeeModel = new EmployeeModel() {  
  19.                 Name = "Irshad",  
  20.                     Designation = "SE",  
  21.                     Qualification = "MCA"  
  22.             };  
  23.         }  
  24.         private string _message;  
  25.         public string Message {  
  26.             get {  
  27.                 return _message;  
  28.             }  
  29.             set {  
  30.                 _message = value;  
  31.                 OnPropertyChanged();  
  32.             }  
  33.         }  
  34.         public Command SaveCommand {  
  35.             get {  
  36.                 return new Command(() => {  
  37.                     Message = "I am " + EmployeeModel.Name + ", My qualification is " + EmployeeModel.Qualification + " and working as a " + EmployeeModel.Designation;  
  38.                 });  
  39.             }  
  40.         }  
  41.         public event PropertyChangedEventHandler PropertyChanged;  
  42.         protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) {  
  43.             PropertyChanged ? .Invoke(thisnew PropertyChangedEventArgs(propertyName));  
  44.         }  
  45.     }  
  46. }  

Open MainPage.xaml and modify the code as below.

  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:MvvmDemo" x:Class="MvvmDemo.MainPage" BackgroundColor="Cyan">  
  3.     <StackLayout VerticalOptions="Center">  
  4.         <Label Text="Employee name : "></Label>  
  5.         <Entry Text="{Binding EmployeeModel.Name, Mode=TwoWay}"></Entry>  
  6.         <Label Text="Employee Designation : "></Label>  
  7.         <Entry Text="{Binding EmployeeModel.Designation, Mode=TwoWay}"></Entry>  
  8.         <Label Text="Employee Qualification : "></Label>  
  9.         <Entry Text="{Binding EmployeeModel.Qualification, Mode=TwoWay}"></Entry>  
  10.         <Button Text="Save Employee" Command="{Binding SaveCommand}"></Button>  
  11.         <Label Text="{Binding Message}"></Label> </StackLayout>  
  12. </ContentPage>  

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

  1. using Xamarin.Forms;  
  2. using MvvmDemo.ViewModels;  
  3. namespace MvvmDemo {  
  4.     public partial class MainPage: ContentPage {  
  5.         EmployeeViewModel evm;  
  6.         public MainPage() {  
  7.             InitializeComponent();  
  8.             evm = new EmployeeViewModel();  
  9.             BindingContext = evm;  
  10.         }  
  11.     }  
  12. }  

Check the configuration manager. It should be checked with the build and deploy options for the platforms you require.


Xamarin

Folder structure for the portable project will be as below.

Xamarin

Note

I have attached only portable project. You have to create your own project and copy the portable project files from the attached one.

Run the application. You will get the following screen.


Xamarin

Modify the details as shown below.
Xamarin

Click on 'Save Employee' button.

Xamarin

You will get the updated binded value of Employee model details to the message label.

How it works

  1. We have used PropertyChangedEventHandler, that will indentity the method which handles the event. If you want to associate the event with your handler, you have to add the instance of the delegate to the event. This event handler will be called whenever the event occurs.

  2. We have done the binding of Entry View as two-way binding as {Binding EmployeeModel.Name, Mode=TwoWay}. This will set the property value of the binded model as soon as you unfocused the control.

  3. OnPropertyChanged() method is responsible to return and reflect the latest updated value of the property so that it will reflect to the binded View.

  4. SaveCommand: this command is simply used to get the latest value of the updated model that will tell what the updated model is and demonstrate MVVM that the ViewModel value has been changed by changing the value from UI.

Up Next
    Ebook Download
    View all
    Learn
    View all