Tabbed Page In Xamarin.Forms

The following topics are discussed in this article.

  • What is a tabbed page?
  • Uses of a tabbed page.
  • Practical example of a tabbed page

Targeted Audience

Targeted audience is people with basic knowledge of XAML and C#.

What is a tabbed page?

In your mobile, you may use some application that contains different tabs and you can navigate from one tab to another with just a single click. Tabbed page is a page that contains different tabs and each tab has its own title and own content depending upon the type and logic of application.

Xamarin

You can also see it in the image, with different tabs. Let’s discuss some of its uses, and move towards its practical example.

Uses of a tabbed page

  • Access multiple pages by a single page
  • Navigation between pages becomes easier
  • More amount of information is shown on a page.
  • Easy to use
  • One of the most used tabbed page applications is “Instagram”.

Creating a tabbed page

Here, I am using Visual Studio 2017 for Windows OS with Xamarin workload installed in it. Now, we are going to make a tabbed page application. Just follow the following steps.

First make a Xamarin.forms project. If you are new to Xamarin.forms, learn how to create your first cross-platform mobile application using Visual Studio 2017, here.

Now, right click on your portable project -> Add -> New item.

Xamarin

Then, go to Cross-Platform -> Forms Tabbed Page Xaml (rename it) -> click Add.

Xamarin

Now, you will see some code in your tabbed page.

Xamarin

A tab page is a parent page that contains multiple pages in it. You can add child pages in 2 ways.

  • By adding content page tab in your parent tabbed page. You can code multiple content pages in your parent page.
  • Use reference of other page as a child of tabbed page.

The default code generated by making a tabbed page is given below.

XAML

  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <TabbedPage xmlns="http://xamarin.com/schemas/2014/forms"  
  3.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"  
  4.              x:Class="CsharpCorner.TabbedPageExample"  
  5.              xmlns:pages="clr-namespace:CsharpCorner;assembly=CsharpCorner"  
  6.              Title="TabbedPage">  
  7.   <!--Pages can be added as references or inline-->  
  8.   <pages:TabbedPageExampleTab1/>  
  9.   <ContentPage Title="Tab 2">  
  10.     <StackLayout>  
  11.       <Label Text="Green"  HorizontalTextAlignment="Center" HorizontalOptions="FillAndExpand" Margin="5" />  
  12.       <BoxView Color="Green" VerticalOptions="FillAndExpand" />  
  13.     </StackLayout>  
  14.   </ContentPage>  
  15.   <ContentPage Title="Tab 3">  
  16.     <StackLayout>  
  17.       <Label Text="Blue" HorizontalTextAlignment="Center" HorizontalOptions="FillAndExpand" Margin="5"  />  
  18.       <BoxView Color="Blue" VerticalOptions="FillAndExpand" />  
  19.     </StackLayout>  
  20.   </ContentPage>  
  21. </TabbedPage>   

Explaining Code

<pages:TabbedPageExampleTab1/>

This line is used in the above code to add the reference of other page named “TabbedPageExampleTab1”. Now, this page will act as one of the tabs in your tabbed page application.

And, the next code is -

  1. <ContentPage Title="Tab 2">  
  2.   <StackLayout>  
  3.     <Label Text="Green"  HorizontalTextAlignment="Center" HorizontalOptions="FillAndExpand" Margin="5" />  
  4.     <BoxView Color="Green" VerticalOptions="FillAndExpand" />  
  5.   </StackLayout>  
  6. </ContentPage>   

This code will add another content page in your application but this is not the reference of other page. This code will add another content page with a title of Tab 2.

Now, we are going to make some changes in both of the files - Main parent file and the TabbedPageExampleTab1 file. So, we can make a tabbed page according to our desire. Let’s add some content in it.

I have made three tabs named Profile, Home, and Notifications. Now, let's add content in it.

Code of Tab1 

  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="CsharpCorner.TabbedPageExampleTab1"  
  5.              Title="Profile">  
  6.   <StackLayout>  
  7.     <Label Text="Hello. I am umair Hassan Member of C-sharp Corner" Margin="5" />  
  8.         <Image   
  9.             HeightRequest="80"  
  10.             WidthRequest="80"  
  11.             Source="http://csharpcorner.mindcrackerinc.netdna-cdn.com/UploadFile/AuthorImage/2e96ef20160701111857.jpg">  
  12.         </Image>  
  13.   
  14.         <Label Text="You are on Tab one"></Label>  
  15.   
  16.         <BoxView VerticalOptions="FillAndExpand" Color="Silver"></BoxView>  
  17.   </StackLayout>  
  18. </ContentPage>   

Main Tabbed Page 

  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <TabbedPage xmlns="http://xamarin.com/schemas/2014/forms"  
  3.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"  
  4.              x:Class="CsharpCorner.TabbedPageExample"  
  5.              xmlns:pages="clr-namespace:CsharpCorner;assembly=CsharpCorner"  
  6.              Title="TabbedPage">  
  7.   <!--Pages can be added as references or inline-->  
  8.   <pages:TabbedPageExampleTab1/>  
  9.   <ContentPage Title="Home">  
  10.     <StackLayout>  
  11.       <Label Text="You are on Tab 2"  HorizontalTextAlignment="Center" HorizontalOptions="FillAndExpand" Margin="5" />  
  12.       <BoxView Color="Silver" VerticalOptions="FillAndExpand" />  
  13.     </StackLayout>  
  14.   </ContentPage>  
  15.   <ContentPage Title="Notifications">  
  16.     <StackLayout>  
  17.       <Label Text="You are on Tab 3" HorizontalTextAlignment="Center" HorizontalOptions="FillAndExpand" Margin="5"  />  
  18.       <BoxView Color="Lime" VerticalOptions="FillAndExpand" />  
  19.     </StackLayout>  
  20.   </ContentPage>  
  21. </TabbedPage>   

Output on Android and Windows desktop

Xamarin

So, this is the final output of our tabbed page application.