How To Create Pages Of Different Kinds In Xamarin

Introduction

This article tells you about how to create pages in Xamarin, how to work with them, and how they differentiate from each other.

Targeted Audience

People with the basic knowledge of C# and Xamarin.

Tools

Visual Studio with Xamarin installed.

ContentPage

ContentPage is a simple blank page which allows making a single “View” object. It further consists of different layouts like AbsoluteLayout and StackLayout. The example below shows the ContentPage having a label.

How To Create Page In Xamarin 

Remember that a ContentPage can have only one child at a time. If you try to add another one, it will result in an error. To add more children, you can use layouts, which allows the addition of multiple children. You can add a Button, Entry, Label, Image, or any other type of View as you want. Also, you can set colors, background colors, and other styles.

XAML 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.              xmlns:local="clr-namespace:PracApp1"  
  5.              x:Class="PracApp1.MainPage">  
  6.     <Label HorizontalOptions="Center" VerticalOptions="Center" Text="Label"></Label>  
  7. </ContentPage>  
C# Code
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using Xamarin.Forms;  
  7.   
  8. namespace PracApp1  
  9. {  
  10.     public partial class MainPage : ContentPage  
  11.     {  
  12.         public MainPage()  
  13.         {  
  14.             InitializeComponent();  
  15.    
  16.         }  
  17.     }  
  18. }  

TabbedPage

TabbedPage contains multiple tabs and detailed areas for each of them. Each tab loads its contents into the detail area. You can navigate between these tabs. It is necessary to add navigation between pages in most of the cases but for this example, we are using a simple tabbed page. The picture below shows a tabbed page with four tabs and the detail area.

How To Create Page In Xamarin
 
How To Create Page In Xamarin 

The tab of a tabbed page can consist of a content page or any other page. So, you can work any way you want in that.

XAML Code
  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.              xmlns:local="clr-namespace:PracApp1"  
  5.              x:Class="PracApp1.MainPage">  
  6.   
  7.     <ContentPage BackgroundColor="Red" Title="Tab1">  
  8.         <Label HorizontalOptions="Center" VerticalOptions="Center" Text="This is Tab 1"></Label>  
  9.     </ContentPage>  
  10.   
  11.     <ContentPage BackgroundColor="Green" Title="Tab2">  
  12.         <Label HorizontalOptions="Center" VerticalOptions="Center" Text="This is Tab 2"></Label>  
  13.     </ContentPage>  
  14.   
  15.     <ContentPage BackgroundColor="Blue" Title="Tab3">  
  16.         <Label HorizontalOptions="Center" VerticalOptions="Center" Text="This is Tab 3"></Label>  
  17.     </ContentPage>  
  18.   
  19.     <ContentPage BackgroundColor="Aqua" Title="Tab4">  
  20.         <Label HorizontalOptions="Center" VerticalOptions="Center" Text="This is Tab 4"></Label>  
  21.     </ContentPage>  
  22.   
  23. </TabbedPage>  
C# Code
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using Xamarin.Forms;  
  7.   
  8. namespace PracApp1  
  9. {  
  10.     public partial class MainPage : TabbedPage  
  11.     {  
  12.         public MainPage()  
  13.         {  
  14.             InitializeComponent();  
  15.         }  
  16.     }  
  17. }  

MasterDetailPage

MasterDetail Page consists of a master portion which is normally on the left side of the screen and consists of a list of menus. Each of them contains the detail section just like the TabbedPage. MasterDetailPage is pretty similar to the TabbedPage but in TabbedPage, you make pages within the TabbedPage while in MasterDetailPage, you code for the Master and the Detail portion separately. You have to add references for each of the pages to the master portion which further navigates on clicking to the targeted page. It sounds very difficult but it's easy to make it now because Visual Studio gives an option to build MasterDetailPage. You have to follow the following steps.

  • Open Visual Studio and create a new project. Select cross-platform and click OK.

    How To Create Page In Xamarin

  • When the pop up opens to ask for the page type, select MasterDetailPage and click OK. The page will automatically get created; you have to do nothing to build it.

    How To Create Page In Xamarin

  • When the project is created, it will look like the picture below.

    How To Create Page In Xamarin

The master portion is already linked with the view:MenuPage file and the Detail portion with the view:ItemPage file. You can simply write the code for the master in the MenuPage.xaml.

CarouselPage

CarouselPage allows you to swipe left or right to navigate through the pages. It is also similar to the TabbedPage but does not consist of tabs. Simply, create some pages according to your need and go to App.cs to add them to the CarouselPage. The code below shows you how to easily create a CarouselPage.

XAML 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.              xmlns:local="clr-namespace:PracApp1"  
  5.              x:Class="PracApp1.MainPage"  
  6.              BackgroundColor="Red">  
  7.   
  8.     <ContentPage.Content>  
  9.         <StackLayout>  
  10.             <Label Text="This is Page 1"  
  11.                 VerticalOptions="CenterAndExpand"   
  12.                 HorizontalOptions="CenterAndExpand" />  
  13.         </StackLayout>  
  14.     </ContentPage.Content>  
  15. </ContentPage>  

For second page,

  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="PracApp1.Page1"  
  5.              BackgroundColor="Green">  
  6.     <ContentPage.Content>  
  7.         <StackLayout>  
  8.             <Label Text="This is Page 2"  
  9.                 VerticalOptions="CenterAndExpand"   
  10.                 HorizontalOptions="CenterAndExpand" />  
  11.         </StackLayout>  
  12.     </ContentPage.Content>  
  13. </ContentPage>  

For third page,

  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="PracApp1.Page2"  
  5.              BackgroundColor="Blue">  
  6.     <ContentPage.Content>  
  7.         <StackLayout>  
  8.             <Label Text="Welcome to Xamarin.Forms!"  
  9.                 VerticalOptions="CenterAndExpand"   
  10.                 HorizontalOptions="CenterAndExpand" />  
  11.         </StackLayout>  
  12.     </ContentPage.Content>  
  13. </ContentPage>  
C# Code
  1. using System;  
  2. using Xamarin.Forms;  
  3. using Xamarin.Forms.Xaml;  
  4.   
  5. [assembly: XamlCompilation(XamlCompilationOptions.Compile)]  
  6. namespace PracApp1  
  7. {  
  8.     public partial class App : Application  
  9.     {  
  10.         public App()  
  11.         {  
  12.             InitializeComponent();  
  13.   
  14.             //MainPage = new MainPage();  
  15.             CarouselPage carouselPage = new CarouselPage();  
  16.             carouselPage.Children.Add(new MainPage());  
  17.             carouselPage.Children.Add(new Page1());  
  18.             carouselPage.Children.Add(new Page2());  
  19.   
  20.             MainPage = carouselPage;  
  21.         }  
  22.   
  23.         protected override void OnStart()  
  24.         {  
  25.             // Handle when your app starts  
  26.         }  
  27.   
  28.         protected override void OnSleep()  
  29.         {  
  30.             // Handle when your app sleeps  
  31.         }  
  32.   
  33.         protected override void OnResume()  
  34.         {  
  35.             // Handle when your app resumes  
  36.         }  
  37.     }  
  38. }  

Summary

This article demonstrated how to make different kinds of pages in Xamarin and what are the differences between them. I hope this will help you to know how to make pages, with a better understanding. 


Similar Articles