Dynamic Master Detail Page In Xamarin.Forms

Introduction

This article demonstrates how to create and use a Dynamic Master Detail Page in Xamarin.Forms, XAML, and C#. This article starts with the introduction of the Master Detail Page tag in XAML.

Xamarin

Today I am going to tell you about master detail page.  I have faced so many problems while creating it. 

After a long struggle, I came up with some easy steps to create master detail page. Before moving towards implementation let's have some short details about the terms.

The master detail page is a page in which we are creating menus to navigate through the application such as Home, Setting, About, Help, Logout, etc.

Master detail menus are generally used in most mobile applications and it also looks attractive.

We can customize the master-detail page in our own way, and make it attractive.

Let's begin.

Definition

Master Detail Page: A page that displays the information in two parts, higher level and lower level.

In A Simple Way

Master detail page contains two things, one is a master page and another is the detail page.

A master page contains the menu list and the detail page that displays the details and provides the link to go back to the master page.

The detail page is a content holder that can hold another page which is linked to the master page’s menus. 

Xamarin

Implementation 

Open Visual Studio and select a New Project.

Xamarin

Now, select Cross-Platform App, give the project a name and set the project path. Then click OK.

Xamarin

Select the template as "Blank App" and code sharing as "PCL".

Xamarin

Step-1

Create a folder which is named MenuItems and Views.

Xamarin

Right-click on MenuItems folder and select Add >> New Item or Add >> Class. 

Xamarin

Step-2

We create a class MasterPageItem.cs and write the following C# code.

MasterPageItem.cs

  1. public class MasterPageItem {  
  2.     public string Title {  
  3.         get;  
  4.         set;  
  5.     }  
  6.     public string Icon {  
  7.         get;  
  8.         set;  
  9.     }  
  10.     public Type TargetType {  
  11.         get;  
  12.         set;  
  13.     }  
  14. }  

Step 3

In Views folder, you can create three pages to navigate through the detail page. (Ctr+Sht+A is short cut key for adding New Items) .

Like this =>

  1. Home Page
  2. Setting Page
  3. Help Page
  4. Logout Page

Step 4

Now,  we  will set up the Main Page for the master-detail page and set the list view on the master detail page.

  1. <MasterDetailPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:XFMasterDetailPageNavigation" x:Class="NavigationMasterDetail.MainPage">  
  2.     <MasterDetailPage.Master>  
  3.         <ContentPage Title="Menu">  
  4.             <Grid BackgroundColor="Transparent">  
  5.                 <Grid.RowDefinitions>  
  6.                     <RowDefinition Height="200" />  
  7.                     <RowDefinition Height="*" /> </Grid.RowDefinitions>  
  8.                 <Grid>  
  9.                     <Image Source="bg.png" Aspect="AspectFill" />  
  10.                     <StackLayout Padding="0,20,0,0" HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand">  
  11.                         <Image BorderColor="White" Source="profile.png" Aspect="AspectFill" WidthRequest="85" HeightRequest="85" />  
  12.                         <Label Text="Adam Pedley" TextColor="White" FontSize="Large" /> </StackLayout>  
  13.                 </Grid>  
  14.                 <StackLayout Margin="20,20,20,0" Grid.Row="1" Spacing="15">  
  15.                     <ListView x:Name="navigationDrawerList" RowHeight="60" SeparatorVisibility="None" BackgroundColor="#e8e8e8" ItemSelected="OnMenuItemSelected">  
  16.                         <ListView.ItemTemplate>  
  17.                             <DataTemplate>  
  18.                                 <ViewCell>  
  19.                                     <!-- Main design for our menu items -->  
  20.                                     <StackLayout VerticalOptions="FillAndExpand" Orientation="Horizontal" Padding="20,10,0,10" Spacing="20">  
  21.                                         <Image Source="{Binding Icon}" WidthRequest="40" HeightRequest="40" VerticalOptions="Center" />  
  22.                                         <Label Text="{Binding Title}" FontSize="Medium" VerticalOptions="Center" TextColor="Black" /> </StackLayout>  
  23.                                 </ViewCell>  
  24.                             </DataTemplate>  
  25.                         </ListView.ItemTemplate>  
  26.                     </ListView>  
  27.                 </StackLayout>  
  28.             </Grid>  
  29.         </ContentPage>  
  30.     </MasterDetailPage.Master>  
  31.     <MasterDetailPage.Detail>  
  32.         <NavigationPage> </NavigationPage>  
  33.     </MasterDetailPage.Detail>  
  34. </MasterDetailPage> 

Please make sure of the menu title name.

Step 5

Now, write some code in the master page.

  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 XFMasterDetailPageNavigation {  
  8.     public partial class MainPage: MasterDetailPage {  
  9.         public List < MasterPageItem > menuList {  
  10.             get;  
  11.             set;  
  12.         }  
  13.         public MainPage() {  
  14.             InitializeComponent();  
  15.             menuList = new List < MasterPageItem > ();  
  16.             // Adding menu items to menuList and you can define title ,page and icon  
  17.             menuList.Add(new MasterPageItem() {  
  18.                 Title = "Home", Icon = "homeicon.png", TargetType = typeof(TestPage1)  
  19.             });  
  20.             menuList.Add(new MasterPageItem() {  
  21.                 Title = "Contact", Icon = "contacticon.png", TargetType = typeof(TestPage2)  
  22.             });  
  23.             menuList.Add(new MasterPageItem() {  
  24.                 Title = "About", Icon = "abouticon.png", TargetType = typeof(TestPage3)  
  25.             });  
  26.             menuList.Add(new MasterPageItem() {  
  27.                 Title = "Main", Icon = "icon.png", TargetType = typeof(TestPage1)  
  28.             });  
  29.             // Setting our list to be ItemSource for ListView in MainPage.xaml  
  30.             navigationDrawerList.ItemsSource = menuList;  
  31.             // Initial navigation, this can be used for our home page  
  32.             Detail = new NavigationPage((Page) Activator.CreateInstance(typeof(TestPage1)));  
  33.         }  
  34.         // Event for Menu Item selection, here we are going to handle navigation based  
  35.         // on user selection in menu ListView  
  36.         private void OnMenuItemSelected(object sender, SelectedItemChangedEventArgs e) {  
  37.             var item = (MasterPageItem) e.SelectedItem;  
  38.             Type page = item.TargetType;  
  39.             Detail = new NavigationPage((Page) Activator.CreateInstance(page));  
  40.             IsPresented = false;  
  41.         }  
  42.     }  
  43. }  

 TA-DA!

Xamarin

I hope that you will like this type of navigation drawer (master-detail page) in your Xamarin.Forms App.

If you want the full source code Click Here.

X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now