Basic Structure Of A Mobile App Using Xamarin MVVM - Part Two

Introduction 

 
This is part 2 of the MVVM Mobile App development series.
 
In this part, we'll discuss navigation in Xamarin forms. We already set up our home page in the previous part. So far, if you haven't seen part 1, you can easily visit the link here.
 
Okay, so now we'll start part 2.
 
Step 1
 
Go to the root directory where you saved your app, open the project, and run your app in the Android emulator. I'm using 'API 29 (Android 10.0)'. You can use any version, but my suggestion is to use this version for better performance. Even though you can use your external Android device, for this you will have to do some configuration. For sure I'll write one blog to set up an external android device with the visual studio when we will discuss Web API and how to connect them to the client-side using the localhost. Finally, we are running our application as shown in the below image.
 
Basic Structure of Mobile App Using Xamarin MVVM
 
Step 2
 
Create a new XAML page and ViewModel class for the existing HomePage.xaml because we have to navigate from the home page to another page. Follow the below steps.
 
Right-click on Views Folder -> Add -> New Item, under 'visual c# items' Select 'Content Page' and give it a name. I named it NewNavigationPage.xaml.
 
To create a ViewModel class for the respective XAML page, follow these steps:
 
Right click on ViewModels folder -> Add -> New Item, under 'visual c# items' select class and name it as HomeViewModel.cs
 
Follow the same step to create ViewModel class(NewNavigationViewModel.cs) for NewNavigationPage.xaml.
 
Step 3
 
Now register the route for the NewNavigationPage in AppShell.xaml.cs
 
Open AppShell.xaml.cs and update it with the below code:
  1. using System;  
  2. using System.Collections.Generic;  
  3. using MvvmApp.ViewModels;  
  4. using MvvmApp.Views;  
  5. using Xamarin.Forms;  
  6.   
  7. namespace MvvmApp  
  8. {  
  9.     public partial class AppShell : Xamarin.Forms.Shell  
  10.     {  
  11.         public AppShell()  
  12.         {  
  13.             InitializeComponent();  
  14.             Routing.RegisterRoute(nameof(ItemDetailPage), typeof(ItemDetailPage));  
  15.             Routing.RegisterRoute(nameof(NewItemPage), typeof(NewItemPage));  
  16.             Routing.RegisterRoute(nameof(NewNavigationPage), typeof(NewNavigationPage));  
  17.         }  
  18.   
  19.     }  
  20. }  
Step 4
 
Create a Command to bind it with a button on the XAML page and create one method to perform navigation action in HomeViewModel.cs . Open HomeViewModel.cs and update it with the below code:
  1. using MvvmApp.Views;  
  2. using System;  
  3. using System.Collections.Generic;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using System.Windows.Input;  
  7. using Xamarin.Forms;  
  8.   
  9. namespace MvvmApp.ViewModels  
  10. {  
  11.     public class HomeViewModel  
  12.     {  
  13.         public ICommand NewNavigationCommand { getset; }  
  14.   
  15.         public HomeViewModel()  
  16.         {  
  17.             NewNavigationCommand = new Command(async () => await NewNavigationAction());  
  18.         }  
  19.   
  20.         async Task NewNavigationAction()  
  21.         {  
  22.             await Shell.Current.GoToAsync(nameof(NewNavigationPage));  
  23.         }  
  24.     }  
  25. }  
Step 5
 
Now create a button in HomePage.xaml and bind it with the Command of HomePageViewModel.cs 
  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.              x:Class="MvvmApp.Views.HomePage"    
  5.              xmlns:vm="clr-namespace:MvvmApp.ViewModels">    
  6.     <ContentPage.BindingContext>    
  7.         <vm:HomeViewModel/>    
  8.     </ContentPage.BindingContext>    
  9.     <ContentPage.Content>    
  10.             
  11.         <StackLayout>    
  12.             <Label Text="Welcome to Home Page of MVVM App!"     
  13.                    TextColor="Black" FontSize="20"    
  14.                 VerticalOptions="CenterAndExpand"     
  15.                 HorizontalOptions="CenterAndExpand" />    
  16.             <Button Text="Click to Navigate" Command="{Binding NewNavigationCommand}"     
  17.                     HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand"/>    
  18.         </StackLayout>    
  19.     </ContentPage.Content>    
  20. </ContentPage>     
In this step, we bind our ViewModel with View. There are several ways to do this, but personally, I always prefer to bind it in view as I did in the above code. This is because if you created properties and command in the ViewModel class, the intelliSence will always show that props and commands while you are binding any content of ViewModel. As shown below:
  1. <ContentPage.BindingContext>    
  2.    <vm:HomeViewModel/>    
  3. </ContentPage.BindingContext>   
Step 6
 
Open NewNavigationPage.xaml and update with the below code:
  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.              x:Class="MvvmApp.Views.NewNavigationPage">    
  5.     <ContentPage.Content>    
  6.         <StackLayout>    
  7.             <Label Text="Welcome to New Navigation Page"    
  8.                 VerticalOptions="CenterAndExpand"     
  9.                 HorizontalOptions="CenterAndExpand" />    
  10.         </StackLayout>    
  11.     </ContentPage.Content>    
  12. </ContentPage>     
Step 7
 
Now run your app in the Android emulator. It will look like this:
 
Basic Structure of Mobile App Using Xamarin MVVM
 
Now click on the button and it will navigate you to the NewNavigationPage.xaml:
 
Basic Structure of Mobile App Using Xamarin MVVM
 
Thanks for giving your precious time, if it won't work for you or you are stuck somewhere, just let me know in the comments section.