Using MasterPage In Xamarin.Form

Introduction

Xamarin is a platform that allows us to create multi-platform mobile applications like Windows phone, Apple iOS, and Android through a single integrated development environment (IDE). We will discuss how to create MasterDetail Page applications from VS2017. There are many plugins available for Xamarin Cross platform including Stack Layout, Label,ScrollView and buttons.

STEP 1

First, select FILE >> NEW >> PROJECT>>Cross Platform App(Xamarin.Form or Native)>>CHANGE YOUR OWN Name then click OK button .

 

Step2

When New Cross Platform App is created then click on BlankApp. We need to select xamarin.forms next select Portable class library (PCL) go to click on button.

 

Step3

Select MaterDetailPageDemo right click to more options available select add option extract to click on New Item…

 

Step4

Next appears New item list and we will select Cross Platform. Then click on Form; then blank content page.Xaml form; then  visual C# change your name and click on OK button

 

Step5

We will select Homepage.Xaml Default Mainpage.Xaml is available but choose a meaningful name for the homepage.

 

I have a code to generate the program otherwise you will prepare own code to generate the program.But you have to create a new one including StackLayout .

Source Code

  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="MasterDetailPageDemo.HomePage" BackgroundColor="White"> b  
  3.     <StackLayout>  
  4.         <Label Text="Home" FontSize="Medium" TextColor="Red" VerticalOptions="Center" HorizontalOptions="Center" />  
  5.         <Label Text="All Device Access" TextColor="Gray" FontSize="Medium" FontAttributes="Bold" VerticalOptions="Center" HorizontalOptions="Center" />  
  6.         <Button Text="Click" Clicked="Button_Clicked" /> </StackLayout>  
  7. </ContentPage>  

step6


 

CS 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. namespace MasterDetailPageDemo {  
  8.     public partial class HomePage: ContentPage {  
  9.         public HomePage() {  
  10.             InitializeComponent();  
  11.         }  
  12.         private void Button_Clicked(object sender, EventArgs e) {  
  13.             DisplayAlert("Alaram""Main""Tryagain");  
  14.         }  
  15.     }  
  16. }  

Step7

All HomePage t o the MenuPage.Xaml.

 

I have a code to generate the program otherwise you will prepare your own code to generate the program

Xaml Code 
  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <MasterDetailPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="MasterDetailPageDemo.MenuPage" xmlns:local="clr-namespace:MasterDetailPageDemo;assembly=MasterDetailPageDemo">  
  3.     <MasterDetailPage.Master>  
  4.         <ContentPage Title="Menu">  
  5.             <StackLayout Orientation="Vertical">  
  6.                 <Button Text="News" />  
  7.                 <Button Text="Android" />  
  8.                 <Button Text="UWP" />  
  9.                 <Button Text="IOS" />  
  10.                 <Button Text="WinStore" /> </StackLayout>  
  11.         </ContentPage>  
  12.     </MasterDetailPage.Master>  
  13.     <MasterDetailPage.Detail>  
  14.         <local:HomePage/> </MasterDetailPage.Detail>  
  15. </MasterDetailPage>  

Step8

Then go to the MenuPage.Xaml.cs fill in some condition or content.

 

Source 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. namespace MasterDetailPageDemo {  
  8.     public partial class MenuPage: MasterDetailPage {  
  9.         public MenuPage() {  
  10.             InitializeComponent();  
  11.         }  
  12.     }  
  13. }  
Step9

Click on calling or App.cs Page. App page is declared within Home Page and Menu page calling condition.So Compile process completes to create a masterpage

 
Code 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. using Xamarin.Forms;  
  7.   
  8. namespace MasterDetailPageDemo  
  9. {  
  10.     public class App : Application  
  11.     {  
  12.         public App()  
  13.         {  
  14.             // The root page of your application  
  15.             MainPage = new MenuPage();  
  16.         }  
  17.   
  18.         protected override void OnStart()  
  19.         {  
  20.             // Handle when your app starts  
  21.         }  
  22.   
  23.         protected override void OnSleep()  
  24.         {  
  25.             // Handle when your app sleeps  
  26.         }  
  27.   
  28.         protected override void OnResume()  
  29.         {  
  30.             // Handle when your app resumes  
  31.         }  
  32.     }  
  33. }  
 

Step10

Then click on Build Button.  Build operation is for sets of packages and compiling processes any error or warning message in the display. 

 

Select your execution part click on the execution devices then compiling and deploy process will start. Connect mobile to computer if needed 

 

Then start deploying 5”Kitkat (4.4)XXHOP phone display just a few seconds  

Click to DisplayAlertMessage 

 

StackLayout including NEW,ANDROID,IOS,UWP,WINSTORE this button will be shown in MenuPage this concept is called MasterPage.

 

Later we will discuss more Xamarin.Form Applications

SUMMARY

In this article, you learned how to create a MasterPage in app using the Xamarin.Forms application development feature of Xamarin with .NET Standard libraries.

If you have any questions/ feedback/ issues, please write in the comment box.

Up Next
    Ebook Download
    View all
    Learn
    View all