Hamburger Menu In UWP With XAML And C#

Before reading this article, please go through the following article.

  1. Introduction To Universal Windows Platform (UWP) App Development Using Windows 10 And Visual Studio 2015
  2. How to add UWP ToolKit controls in Universal Application Development With XAML AND C#

The hamburger menu is used for navigation. It supports three-tier navigation, filled with drop-downs, social icons, search bars, and utility links.

Reading this article, you can learn about UWP Tool Kit Control – Hamburger Menu in Universal Windows Apps development with XAML and Visual C#.

The following important tools are required for developing UWP,

  1. Windows 10 (Recommended)
  2. Visual Studio 2015 Community Edition (It is a free software available online)

Now, we can discuss step by step app development.

Step 1 - Open Visual Studio 2015 -> Start -> New Project -> select Universal (under Visual C# -> Windows) -> Blank App -> give it a suitable name (UWP Hamburger Menu) -> OK.

  

Step 2 - Choose the Target and Minimum platform version that your UWP application will support. After, the Project creates App.xaml and MainPage.xaml.

  

For adding UWPToolKit Control, please refer.

Step 3 - Add TextBlock control and change the Name and Text property for title.

  

Step 4 - Add images in the Assets folder, and images for Hamburger Menu.

  

Step 5 - Add the following XAML code for creating Local Resource for adding menu items, used to add Hamburger Menu control,

  1. <Page.Resources>  
  2.     <DataTemplate x:Name="HamburDefaultTemplate">  
  3.         <Grid Width="240" Height="48">  
  4.             <Grid.ColumnDefinitions>  
  5.                 <ColumnDefinition Width="48" /> </Grid.ColumnDefinitions>  
  6.             <SymbolIcon Grid.Column="0" Symbol="{Binding Icon}" Foreground="White" />  
  7.             <TextBlock Grid.Column="1" Text="{Binding Name}" FontSize="16" VerticalAlignment="Center" Foreground="White" /> </Grid>  
  8.     </DataTemplate>  
  9. </Page.Resources>  
  
Step 6 - Add UWP Tool Kit Name Space in Mainpage.xaml,

xmlns:Controls="using:Microsoft.Toolkit.Uwp.UI.Controls"


Add Hamburger Menu control from UWP Tool Kit and Change the name property,


Step 7 - Add a frame inside Hamburger Menu,

  

Step 8 - Set the Item template for Hamburger Menu.

  

Step 9 - Create an Item_click event for Hamburger Menu.

  

Step 10 - Add Relative Panel for displaying image,

  

Note - Automatically, the following code will be generated in XAML code View when we are done in the design View.
  1. <Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:UWPHamburgerMenu" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:Controls="using:Microsoft.Toolkit.Uwp.UI.Controls" x:Class="UWPHamburgerMenu.MainPage" mc:Ignorable="d">  
  2.     <Page.Resources>  
  3.         <DataTemplate x:Name="HamburDefaultTemplate">  
  4.             <Grid Width="240" Height="48">  
  5.                 <Grid.ColumnDefinitions>  
  6.                     <ColumnDefinition Width="48" />  
  7.                     <ColumnDefinition /> </Grid.ColumnDefinitions>  
  8.                 <SymbolIcon Grid.Column="0" Symbol="{Binding Icon}" Foreground="White" />  
  9.                 <TextBlock Grid.Column="1" Text="{Binding Name}" FontSize="16" VerticalAlignment="Center" Foreground="White" /> </Grid>  
  10.         </DataTemplate>  
  11.     </Page.Resources>  
  12.     <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">  
  13.         <TextBlock x:Name="tblTitle" HorizontalAlignment="Left" Margin="222,34,0,0" TextWrapping="Wrap" Text="Hamburger Control Test" VerticalAlignment="Top" FontWeight="Bold" FontSize="18" />  
  14.         <Controls:HamburgerMenu x:Name="HBMTest" HorizontalAlignment="Left" Margin="10,66,0,0" VerticalAlignment="Top" ItemTemplate="{StaticResource HamburDefaultTemplate}" ItemClick="HBMTest_ItemClick" PaneBackground="Black" Foreground="White">  
  15.             <Frame x:Name="contentFrame" Margin="0,0,61,0" /> </Controls:HamburgerMenu>  
  16.         <RelativePanel x:Name="RPHUMImage" Margin="222,66,0,0" /> </Grid>  
  17. </Page>  
Step 11 - Add namespace and the following code in MainPage.xaml.cs,
  1. using Windows.UI.Xaml.Media.Imaging;  
  2. public class HMenuItem {  
  3.     public Symbol Icon {  
  4.         get;  
  5.         set;  
  6.     }  
  7.     public string Name {  
  8.         get;  
  9.         set;  
  10.     }  
  11. }  
  12. public sealed partial class MainPage: Page {  
  13.         public MainPage() {  
  14.             this.InitializeComponent();  
  15.             var items = new List < HMenuItem > ();  
  16.             items.Add(new HMenuItem() {  
  17.                 Icon = Symbol.Accept,  
  18.                     Name = "UWP"  
  19.             });  
  20.             items.Add(new HMenuItem() {  
  21.                 Icon = Symbol.Accept,  
  22.                     Name = "Azure"  
  23.             });  
  24.             items.Add(new HMenuItem() {  
  25.                 Icon = Symbol.Accept,  
  26.                     Name = "IoT"  
  27.             });  
  28.             items.Add(new HMenuItem() {  
  29.                 Icon = Symbol.Accept,  
  30.                     Name = "Holo Lens"  
  31.             });  
  32.             HBMTest.ItemsSource = items;  
  33.         }  
  34.         private void HBMTest_ItemClick(object sender, ItemClickEventArgs e) {  
  35.             var menuItem = e.ClickedItem as HMenuItem;  
  36.             string s = menuItem.Name;  
  37.             ImageBrush myBrush = new ImageBrush();  
  38.             Image image = new Image();  
  39.             if (s == "UWP") {  
  40.                 image.Source = new BitmapImage(new Uri("ms-appx:///Assets//01.png"));  
  41.                 myBrush.ImageSource = image.Source;  
  42.                 RPHUMImage.Background = myBrush;  
  43.             } else if (s == "Azure") {  
  44.                 image.Source = new BitmapImage(new Uri("ms-appx:///Assets//02.png"));  
  45.                 myBrush.ImageSource = image.Source;  
  46.                 RPHUMImage.Background = myBrush;  
  47.             } else if (s == "IoT") {  
  48.                 image.Source = new BitmapImage(new Uri("ms-appx:///Assets//03.jpg"));  
  49.                 myBrush.ImageSource = image.Source;  
  50.                 RPHUMImage.Background = myBrush;  
  51.             } else if (s == "Holo Lens") {  
  52.                 image.Source = new BitmapImage(new Uri("ms-appx:///Assets//04.png"));  
  53.                 myBrush.ImageSource = image.Source;  
  54.                 RPHUMImage.Background = myBrush;  
  55.             }  
  56.         }  
Step 12 - Deploy of your app in Local Machine. The output of the Hamburger Menu App is the following.

  

After clicking Hamburger Menu icon,

  


  

Summary - Now, you have successfully tested UWP Tool Kit - Hamburger MenuControl in Visual C# - UWP environment.

 


Similar Articles