Windows 10 - Split View And Unleashing Hamburger Menu

Windows10

Windows 10 is available for development and it's SDK has been released, Split View is a new addition to XAML in Windows 10. Let's learn about split view control. The split view control can be used to make a nav pane pattern. To build this pattern, developer needs to add an expand/collapse button which is formally called hamburger control.

    Split view control has an expandable pane and a content area. The content area is always visible. The pane can expand and collapse and remain in an open state, and can present itself from either the left side or the right side of an app windows.

Split View had 3 Modes

  1. Overlay

    The pane is hidden until opened. When opened, the pane overlays the content area.

  2. Inline

    The pane is always visible and doesn't overlay the content area. The pane and content areas divide the available screen.

  3. Compact

    The pane is always visible in this mode, which is just wide enough to show icons,

Note: Usually 48 epx wide.

The pane and the content area divide the available screen real estate. Although the standard compact mode doesn't overlay the content area, it can transform to a wider pane to show more content which will overlay the content area.

Let's try to use split view to create a hamburger menu

Step 1: Make sure the you have development environment all set. The first thing you need to do is make sure you are running some version of Windows 10, which you can find on Windows Insider. You need to have Visual Studio 2015 community edition installed as well. Those are only two things that ensure that you have installed.

Step 2:
Open Visual Studio Community Edition 2015. Now that you have opened Visual Studio, you need to select Windows 10 section. In that you need to click on Blank Application and create that.

Step 3: Visual Studio would create the project and now you can see the project listings in Solution Explorer. You would notice that this project does not consist of Windows Phone and Windows Store. There is only one project for both Windows Phone store and other.

Step 4: Go to MainPage.xaml and let's edit some XAML. You can see the Grid tag here, you just need to remove that and let's start our work by adding SplitView control. Split view has two main elements, the pane and the content. The pane represents the menu that you want to interact with and the content represents the main content of your page.

Here is the code,

  1. <SplitView>  
  2.     <SplitView.Pane>  
  3.   
  4.     </SplitView.Pane>  
  5.     <SplitView.Content>  
  6.   
  7.     </SplitView.Content>  
  8. </SplitView>  
Before you add content, you should mention length of pane in compact form and in open form. Also some other properties just like I have done here,
  1. <SplitView x:Name="MySplitView" DisplayMode="CompactOverlay" IsPaneOpen="False"  
  2. CompactPaneLength="50" OpenPaneLength="150">  
Now each of the part needs a container that might be of any type, Grid, StackPanel or anything.

Let's add the grid and a simple TextBlock now.
  1. <SplitView.Content>  
  2.     <Grid>  
  3.         <TextBlock Text="Content of SplitView " FontSize="45" Foreground="White" HorizontalAlignment="Center" VerticalAlignment="Center" />  
  4.     </Grid>  
  5. </SplitView.Content>  
Step 5: Now it's time to create Hamburger Button that would be used to expand or contract the pane and would be used as Menu Button, now hamburger is quiet famous and used in Android and iOS. It's sign is 3 horizontal lines.

Let's add that, remember you need to add that in <SplitView.Pane> tag and don't forget to add a container in that before you add Hamburger Menu Button (StackPanel).
  1. <Button x:Name="HamburgerButton" FontFamily="Segoe MDL2 Assets" Content=""  
  2. Width="50" Height="50" Background="Transparent" Click="HamburgerButton_Click"/>  
You would notice that here click event handler is attached to the xaml of hamburger menu. I will tell you later the purpose of this click event

Step 6: You may add some more buttons below hamburger menu button, just like this,
  1. <StackPanel Orientation="Horizontal">  
  2.     <Button x:Name="MenuButton1" FontFamily="Segoe MDL2 Assets" Content="î ¥" Width="50" Height="50" Background="Transparent" />  
  3.     <TextBlock Text="Button 1" FontSize="18" VerticalAlignment="Center" />  
  4. </StackPanel>  
  5. <StackPanel Orientation="Horizontal">  
  6.     <Button x:Name="MenuButton2" FontFamily="Segoe MDL2 Assets" Content="" Width="50" Height="50" Background="Transparent" />  
  7.     <TextBlock Text="Button 2" FontSize="18" VerticalAlignment="Center" />  
  8. </StackPanel>  
  9. <StackPanel Orientation="Horizontal">  
  10.     <Button x:Name="MenuButton3" FontFamily="Segoe MDL2 Assets" Content="" Width="50" Height="50" Background="Transparent" />  
  11.     <TextBlock Text="Button 3" FontSize="18" VerticalAlignment="Center" />  
  12. </StackPanel>  
Now our xaml is ready, you just need one more line in C# to make your Hamburger Menu working

Step 7: You just need to add this simple line to the click event of hamburger menu button.
  1. MySplitView.IsPaneOpen = !MySplitView.IsPaneOpen;  
Conclusion

This is everything you need to make your own hamburger menu. I hope you learnt something good today. Till the next article, Happy coding!

Screenshots

output

hamburger demo

References:

 

Code on Github.