Tabbed Page In Xamarin.Forms Using FreshMvvm

Introduction

Today I am going to post my first article on C# Corner. Being a developer I have been thinking to do a write up here as this is the place where people can get things out of the box for every technology.

In this article we are going see how to make Tabbed Page in Xamarin.Forms using FreshMvvm.

FreshMvvm

According to Github, FreshMvvm is a super light Mvvm Framework designed specifically for Xamarin.Forms. It's designed to be simple and flexible. It has many built-in features, for example automatic wiring up of  Binding Context and PageModel to PageModel Navigation, just to name a few. I hope I do not need to mention about Xamarin.Forms J.

Requirement

  • Using Microsoft Visual Studio Enterprise 2017 (VS Community as well can be used) along with the installed latest version of Xamarin.
  • Using Windows 10 & macOS Mojave machine in order to see output in for both Android & iOS platforms (Mac is not mandetory)

Detailed description

Open VS Goto ---File—New –Project

Tabbed Page In Xamarin.Forms Using FreshMvvm 

Afer clicking Ok button you will see Cross platform app screen. Select the same options as the below screenshot & press the ok button.

Tabbed Page In Xamarin.Forms Using FreshMvvm 

In the above screenshot, in Platform option I am not including Windows (UWP) option; if you wish to select that option you can do so. It doesn’t take anything extra.

Now that your projects have been created sucessfully, add FreshMvvm packages. Right click on solution, select Manage NuGet packages. By going to NuGet Package manager a window would appear. Search & add the FreshMvvm Package. Refer to the screenshot below,

Tabbed Page In Xamarin.Forms Using FreshMvvm 

Now in your Common project (Shared project) add the below folders

  • Pages
  • PageModels

Note: In FreshMvvm Pages folder has all the Pages (Views) and suffix Page & PageModel folder have all the PageModels(ViewModel) with suffix PageModel.

All FreshMvvm all pages should  end with rge name Page, and PageModels class should end  with PageModel. This is required for automatic event wiring up. FreshMvvm recognizes PageModel  by its naming convention.

Example

If your page name is FirstPage then  the relevent PageModel name should be FirstPageModel.

Now in Pages folder add two Content Pages (.xaml) for two tabs:

  • HomeTabPage
  • AboutTabPage

In PageModels folder add two .cs classes with names:

  • HomeTabPageModel
  • AboutTabPageModel

After adding these new items your solution would look like this

Tabbed Page In Xamarin.Forms Using FreshMvvm

 

To use FreshMvvm Funcationality we need to extend every PageModel class with FreshBasePageModel by importing FreshMvvm. Write the code below in your relevant files

HomeTabModel.cs

  1. public class HomeTabPageModel:FreshBasePageModel  
  2. {  
  3.     public string HomeLabel  
  4.     {  
  5.         get { return "Hi, this is home page"; }  
  6.     }  
  7. }  

HomeLabel is a public property which I am going to bind in Label in xaml file.

AbouTabPageModel.cs

  1. public class AboutTabPageModel:FreshBasePageModel  
  2. {  
  3.     public string AboutLabel  
  4.     {  
  5.         get { return "Hi, this is about page"; }  
  6.     }  
  7. }  

HomeTabPage.xaml

  1. <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"  
  2.               xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"  
  3.               x:Class="FreshMvvmTab.Pages.HomeTabPage"  
  4.               Title="Home">  
  5.      <ContentPage.Content>  
  6.          <StackLayout>  
  7.              <Label Text="{Binding HomeLabel}"  
  8.                  VerticalOptions="CenterAndExpand"   
  9.                  HorizontalOptions="CenterAndExpand" />  
  10.          </StackLayout>  
  11.      </ContentPage.Content>  
  12.  </ContentPage>  

I bound the above label with HomeLabel, FreshMvvm automatic detects the relevant page model class & binding data.

AbouTabPage.xaml

  1. <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"  
  2.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"              
  3.              x:Class="FreshMvvmTab.Pages.AboutTabPage"  
  4.              Title="About">  
  5.     <ContentPage.Content>  
  6.         <StackLayout>  
  7.             <Label Text="{Binding AboutLabel}"  
  8.                 VerticalOptions="CenterAndExpand"   
  9.                 HorizontalOptions="CenterAndExpand" />  
  10.         </StackLayout>  
  11.     </ContentPage.Content>  
  12. </ContentPage>  

Below we are setting our pages to Tabbed page & Tabbed page to startup page.

App.xaml.cs

  1. public App()  
  2. {  
  3.     InitializeComponent();  
  4.   
  5.     var mainPage = new FreshTabbedNavigationContainer();  
  6.     mainPage.AddTab<HomeTabPageModel>("Home",null);  
  7.     mainPage.AddTab<AboutTabPageModel>("About",null);  
  8.   
  9.     mainPage.BarTextColor = Color.Red;  
  10.     mainPage.BarBackgroundColor = Color.LightBlue;  
  11.     //mainPage.BarBackgroundColor = Color.Red;  
  12.   
  13.     MainPage = mainPage;  
  14. }  

In the code above the AddTab method accepts first param as Tab Text of tab & second is Icon which I am sending null as of now, but you can have the Icon name there. Finally I am setting instance of FreshTabbedNavigationContainer to MainPage. For more information about FreshMvvm visit the link I have given in the beginning of this article.

Have a look at the output:

Tabbed Page In Xamarin.Forms Using FreshMvvm  Tabbed Page In Xamarin.Forms Using FreshMvvm 

If you have any doubts please comment below, I will respond as soon as possible.

Happy coding!


Similar Articles