MVVM In Xamarin.Forms Application For Android And UWP

Model–View–View-Model (MVVM) is a software architectural pattern. An MVVM application has three layers,

  • The Model provides underlying data, sometimes through files or web access.
  • The View is the user interface or presentation layer, generally implemented in XAML.
  • The ViewModel connects the Model and the View.

More information is available at MVVM.

Reading this article, you will learn how to use MVVM architecture in Xamarin.Forms application for Android and Universal Windows Platform with XAML and Visual C#, in cross-platform application development.

The following important tools are required for developing UWP.

  1. Windows 10 (Recommended)
  2. Visual Studio 2017 RC Community/Enterprise/Professional Edition (It is a free trial software available online).
  3. Using Visual Studio 2017 Installer, enable the feature of Mobile development with .NET.

Now, we can discuss step by step app development.

Step 1

Open Visual Studio 2017 RC and go to Start -> New Project-> select Cross-Platform (under Visual C# -> Cross Platform App -> Give a suitable name for your App ( Ex - XamFormMVVM) ->OK.

Step 2

Select the Cross Platform template as Blank APP ->Set UI Technology as Forms and Sharing as PCL. Afterwards, Visual Studio creates 4 projects (Portable, Droid, iOS, UWP) and displays Getting Started.XamarinPage.

 

Step 3

Next, we can implement the MVVM to our project. For adding Model, right click the XamFormMVVM (Portable), Add New Folder named as Model, and add a C# class (Book.cs).


Add the following code to Book.cs.
  1. public class Book {  
  2.     public string BTitle {  
  3.         get;  
  4.         set;  
  5.     }  
  6.     public string BAuth {  
  7.         get;  
  8.         set;  
  9.     }  
  10. }   

Right click the XamFormMVVM (Portable), add New Folder named Service, and add a C# class (BSer.cs).



Add the following namespace and code to BSer.cs.
  1. using XamFormMVVM.Model;  
  2. public class BSer {  
  3.     public List < Book > GetBooks() {  
  4.         var list = new List < Book > {  
  5.             new Book {  
  6.                 BTitle = "The Art of War",  
  7.                     BAuth = " by Sun Tzu"  
  8.             },  
  9.             new Book {  
  10.                 BTitle = "The Four Agreements",  
  11.                     BAuth = " by Miguel Ruiz4"  
  12.             },  
  13.             new Book {  
  14.                 BTitle = "7 Habits of Highly Effective People",  
  15.                     BAuth = " by Stephen R. Covey"  
  16.             },  
  17.             new Book {  
  18.                 BTitle = "How to Win Friends & Influence Peopler",  
  19.                     BAuth = " by Dale Carnegie"  
  20.             },  
  21.             new Book {  
  22.                 BTitle = "Think and Grow Rich",  
  23.                     BAuth = " by Napoleon Hill"  
  24.             },  
  25.             new Book {  
  26.                 BTitle = "Awaken the Giant",  
  27.                     BAuth = " by Anthony Robbins "  
  28.             },  
  29.             new Book {  
  30.                 BTitle = "The Magic of Thinking Big",  
  31.                     BAuth = " by David J. Schwartz"  
  32.             }  
  33.         };  
  34.         return list;  
  35.     }   


For adding ViewModel, right click the XamFormMVVM (Portable), Add new folder named ViewModel, and add a C# class (BViewModel.cs).

Add the following namespace and code to BViewModel.cs.

  1. using System.ComponentModel;  
  2. using XamFormMVVM.Model;  
  3. using XamFormMVVM.Service;  
  4. using System.Runtime.CompilerServices;  
  5. public class BViewModel: INotifyPropertyChanged {  
  6.         private List < Book > _bookList;  
  7.         public List < Book > BookList {  
  8.             get {  
  9.                 return _bookList;  
  10.             }  
  11.             set {  
  12.                 _bookList = value;  
  13.                 OnPropertyChanged();  
  14.             }  
  15.         }  
  16.         public BViewModel() {  
  17.             var BookSer = new BSer();  
  18.             BookList = BookSer.GetBooks();  
  19.         }  
  20.         public event PropertyChangedEventHandler PropertyChanged;  
  21.         protected virtual void OnPropertyChanged([CallerMemberName] string propName = null) {  
  22.             PropertyChanged ? .Invoke(thisnew PropertyChangedEventArgs(propName));  
  23.         }   

For adding the View, right click the XamFormMVVM (Portable), add a new folder named  View, and move the MainPage.Xaml to the View folder. Then, add the following code to MainPage.Xaml.
  1. <xmlns:VM="clr-namespace:XamFormMVVM.ViewModel;assembly=XamFormMVVM">  
  2.     <ContentPage.BindingContext>  
  3.         <VM:BViewModel/> </ContentPage.BindingContext>  
  4.     <StackLayout>  
  5.         <Label Text="Xamarin Form MVVM Demo in Android and UWP " VerticalOptions="Center" HorizontalOptions="Center" />  
  6.         <ListView ItemsSource="{Binding BookList}" HasUnevenRows="True">  
  7.             <ListView.ItemTemplate>  
  8.                 <DataTemplate>  
  9.                     <ViewCell>  
  10.                         <StackLayout Orientation="Horizontal">  
  11.                             <Label Text="{Binding BTitle}" />  
  12.                             <Label Text="{Binding BAuth}" /> </StackLayout>  
  13.                     </ViewCell>  
  14.                 </DataTemplate>  
  15.             </ListView.ItemTemplate>  
  16.         </ListView>  
  17.     </StackLayout>   


Step 4

We will test Android and UWP. So, we can set the Multiple Startup Projects as XamFormMVVM.Droid and XamFormMVVM.UWP (Universal Windows).

Step 5

Change the Configuration Manager settings.
Go to Build -> Configuration Manager, uncheck the "Build" and "Deploy" options for iOS, and check these options for Droid and UWP.

 

Step 6

Deploy your app to Android Emulator and Local Machine (UWP). The output of the XamFormMVVM App is given below.

 

Summary

Now, you have successfully tested MVVM architecture in Xamarin.Forms cross-platform application, using Visual C# and Xamarin.


Similar Articles