Global AppBar For Windows Store Applications

Before reading this article, please go through the following articles-
  1. Developing Windows Store Apps: Introduction And Requirement Specification
  2. Plans For Designing Metro Style Apps (Windows Store Apps)
  3. Windows Store Application Life-Cycle
  4. Guidelines to Pass Windows Store App Certification
  5. Navigation Patterns to Develop Windows Store Application
  6. Templates to Develop Windows Store Apps
  7. Develop Your First Windows Store App (Hello World)
  8. Controls to Design Windows Store Apps
  9. Layout Design Guidelines For Windows Store Apps
  10. Guidelines to Create Splash Screen For Windows Store Application
  11. Configure Your Windows Store Application Using Manifest Designer
  12. Working With Progress Controls in Windows Store Application

Introduction

 
In my last article, we saw the use of progress bar control in a Windows Store app. In this article, we will see how to use a global AppBar for Windows Store applications. As all of you know, in a Windows Store application we have a new control, called AppBar. In a Windows Store app, for each page we can have two app bars, one is the TopAppBar and the second is the BottomAppBar.
 
If you are planning to provide navigation using app bars then we need to add app bars on each page or provide a common command for all pages using an app bar but in this case, you need to repeat the same code for all pages. In this article, we will see how to create a global app bar. We can share the app bar content across pages in the Windows Store application. Let's start by using the following procedure.
 
Step 1
 
First, you need one root page that holds our global application bar, so first, decide which single page you want as your root page. Next, add sub-pages i.e. Page1.xaml and Page2.xaml respectively.
 
Step 2
 
On the root, the page adds the app bar as in the following.
  1. <Page.TopAppBar>    
  2.     <AppBar x:Name="globalAppBar" Padding="10,0,10,0">    
  3.         <Grid>    
  4.             <StackPanel x:Name="leftCommandPanel    
  5.                         Orientation="Horizontal" HorizontalAlignment="Left">    
  6.                 <Button x:Name="Back" Style="{StaticResource BackAppBarButtonStyle}"    
  7.                         AutomationProperties.Name="Back"     
  8.                         Click="Back_Click"/>    
  9.             </StackPanel>    
  10.             <StackPanel x:Name="rightCommandPanel"    
  11.                         Orientation="Horizontal" HorizontalAlignment="Right">    
  12.                 <Button x:Name="page1Button" Content="1"    
  13.                         Style="{StaticResource AppBarButtonStyle}"    
  14.                         AutomationProperties.Name="Page 1"     
  15.                         Click="Page1Button_Click"/>    
  16.                 <Button x:Name="page2Button" Content="2"    
  17.                         Style="{StaticResource AppBarButtonStyle}"    
  18.                         AutomationProperties.Name="Page 2"     
  19.                         Click="Page2Button_Click"/>    
  20.             </StackPanel>    
  21.         </Grid>    
  22.     </AppBar>    
  23. </Page.TopAppBar>    
In the above markup, we have added TopAppBar for our Windows Store app root page. This top app bar contains two stack panels, one for holding the back button and the second for holding links or navigation buttons for the remaining two pages.
 
Step 3
 
Next, add the frame element in your root page that loads the remaining two pages when a specific button is clicked in the app bar.
  1. <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">    
  2.     <Frame x:Name="frame1"/>    
  3. </Grid>   
Step 4
 
Next in the code file of your root page, add the click handler for the app bar command buttons.
  1. Page rootPage = null;    
  2. protected override void OnNavigatedTo(NavigationEventArgs e)    
  3. {    
  4.     rootPage = e.Parameter as Page;    
  5.     frame1.Navigate(typeof(Page1), this);    
  6. }    
  7. private void Back_Click(object sender, RoutedEventArgs e)    
  8. {    
  9.     if (frame1.CanGoBack)    
  10.     {    
  11.         frame1.GoBack();    
  12.     }    
  13.     else if (rootPage != null && rootPage.Frame.CanGoBack)    
  14.     {    
  15.         rootPage.Frame.GoBack();    
  16.     }    
  17. }    
  18. private void Page1Button_Click(object sender, RoutedEventArgs e)    
  19. {    
  20.     frame1.Navigate(typeof(Page1), this);    
  21. }    
  22. private void Page2Button_Click(object sender, RoutedEventArgs e)    
  23. {    
  24.     frame1.Navigate(typeof(Page2), this);    
  25. }    
 
Step 5
 
We are now done with the complete code of the root page. Next on your subpages override the OnNavigatedTo method as in the following.
  1. protected override void OnNavigatedTo(NavigationEventArgs e)  
  2.         {  
  3.             rootPage = (Page)e.Parameter;  
  4.         }   
Step 6
 
Next, run your application and see the global AppBar for your Windows Store application.
 

Conclusion

 
Using this technique we can create the global application bar for Windows Store applications.


Similar Articles