MasterDetailPage In Xamarin Forms Application

Introduction

MasterDetailPage is the main page that is responsible for two related pages. It contains the master page that has items and the detail page that shows the details about the items present on a master page.

In this article, we will see how we can use MasterDetailPage and do navigations between pages having contents.

Implementation

open Visual Studio and select New Project.

MasterDetailPage In Xamarin

Select project type and give this project a name.

MasterDetailPage In Xamarin

Select template – Blank App and code sharing as PCL.

MasterDetailPage In Xamarin

Set the target and minimum platform versions as below.

MasterDetailPage In Xamarin

Set the below configuration.

MasterDetailPage In Xamarin

Open MainPage.xaml and add the follwoing code.

  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <MasterDetailPage xmlns="http://xamarin.com/schemas/2014/forms"  
  3.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"  
  4.              xmlns:local="clr-namespace:MasterDetailsPage"  
  5.              x:Class="MasterDetailsPage.MainPage" Title="I am master" MasterBehavior="Popover" BackgroundColor="Cyan">  
  6.       
  7.     <MasterDetailPage.Master>  
  8.         <ContentPage Padding="10" BackgroundColor="Gray" Title="Master">  
  9.             <ContentPage.Content>  
  10.                 <StackLayout Margin="5,30,5,5">  
  11.                     <Label Text="Master page Menu"></Label>  
  12.                     <Button Text="Add Employee" BackgroundColor="Yellow" Clicked="Button_Clicked"></Button>  
  13.                     <Button Text="List Employee" BackgroundColor="Yellow" Clicked="Button_Clicked2"></Button>  
  14.                     <Button Text="About Us" BackgroundColor="Yellow" Clicked="Button_Clicked3"></Button>  
  15.                     <Button Text="Contact" BackgroundColor="Yellow" Clicked="Button_Clickded4"></Button>  
  16.                 </StackLayout>  
  17.             </ContentPage.Content>  
  18.         </ContentPage>  
  19.     </MasterDetailPage.Master>  
  20.     <MasterDetailPage.Detail>  
  21.         <ContentPage Padding="10">  
  22.             <ContentPage.Content>  
  23.                 <StackLayout Margin="5,30,5,5">  
  24.                     <Label Text="Detail Page"></Label>  
  25.                 </StackLayout>  
  26.             </ContentPage.Content>  
  27.         </ContentPage>  
  28.     </MasterDetailPage.Detail>  
  29. </MasterDetailPage>  

Open MainPage.xaml.cs and modify the content.

  1. using Xamarin.Forms;  
  2.   
  3. namespace MasterDetailsPage  
  4. {  
  5.     public partial class MainPage : MasterDetailPage  
  6.     {  
  7.         public MainPage()  
  8.         {  
  9.             InitializeComponent();  
  10.             Detail = new NavigationPage(new HomePage());  
  11.             IsPresented = false;  
  12.         }  
  13.   
  14.         private void Button_Clicked(object sender, EventArgs e)  
  15.         {  
  16.             Detail = new NavigationPage(new AddEmployee());  
  17.             IsPresented = false;  
  18.         }  
  19.         private void Button_Clicked2(object sender, EventArgs e)  
  20.         {  
  21.             Detail = new NavigationPage(new ListEmployee());  
  22.             IsPresented = false;  
  23.         }  
  24.   
  25.         private void Button_Clicked3(object sender, EventArgs e)  
  26.         {  
  27.             Detail = new NavigationPage(new AboutUs());  
  28.             IsPresented = false;  
  29.         }  
  30.         private void Button_Clickded4(object sender, EventArgs e)  
  31.         {  
  32.             Detail = new NavigationPage(new ContactUs());  
  33.             IsPresented = false;  
  34.         }  
  35.     }  
  36. }  

Add a class file named Employee.cs.

  1. using System.Collections.Generic;  
  2. namespace MasterDetailsPage  
  3. {  
  4.     public class Employee  
  5.     {  
  6.         public string Name { get; set; }  
  7.         public string Address { get; set; }  
  8.         public string Designation { get; set; }  
  9.   
  10.         public List<Employee> getEmployee()  
  11.         {  
  12.             List<Employee> listEmployee = new List<Employee>()  
  13.             {  
  14.                 new Employee() {  
  15.                     Name = "employee1",  
  16.                     Address = "address 1",  
  17.                     Designation = "designation 1"  
  18.                 },  
  19.                 new Employee() {  
  20.                     Name = "employee2",  
  21.                     Address = "address 2",  
  22.                     Designation = "designation 2"},  
  23.                 new Employee() {  
  24.                     Name = "employee3",  
  25.                     Address = "address 3",  
  26.                     Designation = "designation 3"  
  27.                 }  
  28.             };  
  29.             return listEmployee;              
  30.         }  
  31.     }  
  32. }  

Add a ViewModel class file as EmployeeViewModel.cs.

  1. namespace MasterDetailsPage  
  2. {  
  3.     public class EmployeeViewModel  
  4.     {  
  5.         public List<Employee> Employees { get; set; }  
  6.   
  7.         public EmployeeViewModel()  
  8.         {  
  9.             Employees = new Employee().getEmployee();  
  10.         }  
  11.     }  
  12. }  

Add the items files (Xamarin.Forms Content Pages) that will correspond to the given menu items.

  1. Add AboutUs.xaml
    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="MasterDetailsPage.AboutUs" BackgroundColor="Cyan">  
    5.     <ContentPage.Content>  
    6.         <Label Text="About us page" />  
    7.     </ContentPage.Content>  
    8. </ContentPage> 

  1. Add ContactUs.xaml
    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="MasterDetailsPage.ContactUs" Title="Contact" BackgroundColor="Cyan">  
    5.     <ContentPage.Content>  
    6.         <Label Text="Contact us page" />  
    7.     </ContentPage.Content>  
    8. </ContentPage>  

  1. Add HomePage.xaml
    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="MasterDetailsPage.HomePage" Title="Employee" BackgroundColor="Cyan">  
    5.     <ContentPage.Content>  
    6.         <StackLayout>  
    7.             <Label Text="Home page contenet !!!" />  
    8.         </StackLayout>  
    9.     </ContentPage.Content>  
    10. </ContentPage>  

  1. Add AddEmployee.xaml
    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="MasterDetailsPage.AddEmployee" Title="Add Employee" BackgroundColor="Cyan">  
    5.     <ContentPage.Content>  
    6.         <StackLayout Orientation="Vertical" Padding="40">  
    7.             <Label Text="Add new Employee"></Label>  
    8.             <Label Text="Enter Name : "></Label>  
    9.             <Entry x:Name="name" Placeholder="Name"></Entry>  
    10.             <Label Text="Enter Address : "></Label>  
    11.             <Entry x:Name="address" Placeholder="Address"></Entry>  
    12.             <Label Text="Enter Designation : "></Label>  
    13.             <Entry x:Name="designation" Placeholder="Designation"></Entry>  
    14.             <Button Text="Submit" Clicked="Button_Clicked"></Button>  
    15.         </StackLayout>  
    16.     </ContentPage.Content>  
    17. </ContentPage>  

  1. Add DetailsEmployee.xaml
    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="MasterDetailsPage.DetailsEmployee" Title="Details" BackgroundColor="Cyan">  
    5.     <ContentPage.Content>  
    6.         <StackLayout>  
    7.             <Label Text="Details of employee!!" />  
    8.             <Label x:Name="name"></Label>  
    9.             <Label x:Name="address"></Label>  
    10.             <Label x:Name="designation"></Label>  
    11.             <Button Text="Save" Clicked="Button_Clicked"></Button>  
    12.             <Button Text="Edit" Clicked="Button_Clicked_1"></Button>  
    13.         </StackLayout>  
    14.     </ContentPage.Content>  
    15. </ContentPage>  

  1. Add Saved.xaml
    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="MasterDetailsPage.Saved" Title="Saved" BackgroundColor="Cyan">  
    5.     <ContentPage.Content>  
    6.         <StackLayout>  
    7.             <Label Text="Employee Saved !!"/>  
    8.         </StackLayout>  
    9.     </ContentPage.Content>  
    10. </ContentPage>  

  1. Add ListEmployee.xaml
    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="MasterDetailsPage.ListEmployee" Title="List" BackgroundColor="Cyan">  
    5.     <ContentPage.Content>  
    6.         <StackLayout Spacing="10" Padding="10">  
    7.             <Label Text="List of Employee !!" FontSize="20"></Label>  
    8.             <ListView x:Name="listEmployee">  
    9.                 <ListView.ItemTemplate>  
    10.                     <DataTemplate>  
    11.                         <TextCell Text="{Binding Name}" TextColor="Purple" Detail="{Binding Designation}" DetailColor="Maroon"></TextCell>  
    12.                     </DataTemplate>  
    13.                 </ListView.ItemTemplate>  
    14.             </ListView>  
    15.         </StackLayout>  
    16.     </ContentPage.Content>  
    17. </ContentPage>  

Run the application.

  1. You will get the following screen.

    MasterDetailPage In Xamarin

  2. Click the hamburger menu.

    MasterDetailPage In Xamarin

  3. Click "List Employee" menu.

    MasterDetailPage In Xamarin

  1. Click on" Add Employee".

    MasterDetailPage In Xamarin

  2. Fill in the details.

    MasterDetailPage In Xamarin

  1. Click on the "Submit" button.

    MasterDetailPage In Xamarin

  1. Click on the "Save" button.

    MasterDetailPage In Xamarin

  1. Go to the "About" menu.

    MasterDetailPage In Xamarin

  1. Click on "Contact " menu.


    MasterDetailPage In Xamarin

Note

In MainPage.xaml.cs, we can see that the “IsPresented” property has been set to false every time. If we set this property to true, then the menu will be unnecessarily visible to the item content and for making it hidden, we have to click on the item page.

So, in general scenario, we want the menu to be hidden just after selecting the menu item.

You can check the same output on Android Emulator.


Similar Articles