Basic Structure Of Mobile App Using Xamarin MVVM

Introduction

 
This is part 1 of the MVVM Mobile App series.
 
In this part, I'm going to explain the basic structure of a mobile app built in Xamarin using the MVVM pattern. In this part, I'll create a new project and will let you know about predefined libraries and classes of the Xamarin Android app.
 
MVVM stands for Model View ViewModel. I'm using Visual Studio 2019 in this project.
 
Ok, so here we go.
 
Step 1
 
Run Visual Studio and click 'Create a new project' then type in the search box 'Mobile App' and select and select 'Mobile App(Xamarin.forms)' from the list as shown below in the image, then click Next.
 
Basic Structure of Mobile App Using Xamarin MVVM
 
Step 2
 
Chose your app name, in my case it's MvvmApp. Leave all things showing and click 'Create'. It will take you to the template selection window.
 
Select Tabbed and it will create an MVVM blueprint and create some predefined classes. Next, uncheck iOS, we'll learn only Android app development in this series. Press Create, as shown in the below image.
 
Basic Structure of Mobile App Using Xamarin MVVM 
 
Step 3
 
Now you can see your solution window, it contains the Model, View, and ViewModel folder. There is one Xaml file AppShell.xaml which contains the main page of the application with tabs. If you'll check your app.xaml.cs you can see that AppShell is registered as MainPage, as shown in the below image.
 
Basic Structure of Mobile App Using Xamarin MVVM 
 
Step 4
 
We can register our home page in AppShell.xaml, to do so follow the below steps.
  1. Add a HomePage.xaml by clicking right on views folder -> add -> new item
  2. Select Visual C# Items from the left sidebar under the installed tab select Content Page and give it a name in my case its HomePage.xaml. Then click 'Add' as shown in the below image
Basic Structure of Mobile App Using Xamarin MVVM
 
Step 5
 
Now you can see HomePage.xaml is added in the views folder. Now Update HomePage.xaml 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.HomePage">    
  5.     <ContentPage.Content>    
  6.         <StackLayout>    
  7.             <Label Text="Welcome to Home Page of MVVM App!"     
  8.                    TextColor="Black" FontSize="20"    
  9.                 VerticalOptions="CenterAndExpand"     
  10.                 HorizontalOptions="CenterAndExpand" />    
  11.         </StackLayout>    
  12.     </ContentPage.Content>    
  13. </ContentPage>   
Step 6
 
This step is last but not least. Go to AppShell.xaml and update AppShell.xaml with the below code:
  1. <Shell xmlns="http://xamarin.com/schemas/2014/forms"     
  2.        xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"    
  3.        xmlns:local="clr-namespace:MvvmApp.Views"    
  4.        Title="MvvmApp"    
  5.        x:Class="MvvmApp.AppShell">    
  6.     
  7.     <!--    
  8.         The overall app visual hierarchy is defined here, along with navigation.    
  9.         
  10.         https://docs.microsoft.com/xamarin/xamarin-forms/app-fundamentals/shell/    
  11.     -->    
  12.     
  13.     <Shell.Resources>    
  14.         <ResourceDictionary>    
  15.             <Style x:Key="BaseStyle" TargetType="Element">    
  16.                 <Setter Property="Shell.BackgroundColor" Value="{StaticResource Primary}" />    
  17.                 <Setter Property="Shell.ForegroundColor" Value="White" />    
  18.                 <Setter Property="Shell.TitleColor" Value="White" />    
  19.                 <Setter Property="Shell.DisabledColor" Value="#B4FFFFFF" />    
  20.                 <Setter Property="Shell.UnselectedColor" Value="#95FFFFFF" />    
  21.                 <Setter Property="Shell.TabBarBackgroundColor" Value="{StaticResource Primary}" />    
  22.                 <Setter Property="Shell.TabBarForegroundColor" Value="White"/>    
  23.                 <Setter Property="Shell.TabBarUnselectedColor" Value="#95FFFFFF"/>    
  24.                 <Setter Property="Shell.TabBarTitleColor" Value="White"/>    
  25.             </Style>    
  26.             <Style TargetType="TabBar" BasedOn="{StaticResource BaseStyle}" />    
  27.             <Style TargetType="FlyoutItem" BasedOn="{StaticResource BaseStyle}" />    
  28.         </ResourceDictionary>    
  29.     </Shell.Resources>    
  30.     
  31.     <TabBar>    
  32.         <!--<ShellContent Title="About" Icon="tab_about.png" Route="AboutPage" ContentTemplate="{DataTemplate local:AboutPage}" />    
  33.         <ShellContent Title="Browse" Icon="tab_feed.png" ContentTemplate="{DataTemplate local:ItemsPage}" />-->    
  34.     </TabBar>    
  35.         
  36.     <ShellContent Route="HomePage"    
  37.                   ContentTemplate="{DataTemplate local:HomePage}">    
  38.     </ShellContent>    
  39.     <!--    
  40.         If you would like to navigate to this content you can do so by calling    
  41.         await Shell.Current.GoToAsync("//LoginPage");    
  42.     -->    
  43.     <ShellContent Route="LoginPage" Shell.FlyoutBehavior="Disabled" ContentTemplate="{DataTemplate local:LoginPage}" />    
  44.     
  45.     
  46. </Shell>    
Now build your app for the first time and click run with an Android emulator.