Working With The AppBar And Command Bar Control In Universal Window App

 Prerequisites

  •   Visual Studio 2015

Now, let's get started with the steps, given below-

Step 1- Create Windows Universal Project

Open Visual Studio 2015, click File -> New -> Project Option for New Universal app.



Step 2- Giving the Project Name

New Project Window will open, you can select an Installed -> Template -> Visual C# -> Windows -> Universal and select a Blank app (Universal Windows).

Type Project Name AppBar and click OK button.


Step 3- Setting the platform Versions

Here, we choose the Target Version and Minimum Version for our Universal Windows Application. Click OK button.



Step 4- Choose Designer Window

Now, we go to the Solution Explorer and select MainPage.xaml.



Step 5- Designing the App

Drag the Command Bar Control from the tool box.



Next, click Accept button and go the Property Window. Choose the symbol and change the icon to Home.



Change the name as AppHome and label to Home.



Click Cancel button and go the Property Window. Choose the Symbol and change the icon to Contact.



Change the name to AppContact and label to Contact.



Now, drag the app bar button control from the tool box. Click Accept button and go the Property Window. Choose the symbol and change the icon to cancel.



Next, change the name to AppCancel and label to cancel.

 

The following XAML code is generated, while we are designing the app.

 
  1. <Page  
  2.     x:Class="AppBarControl.MainPage"  
  3.     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  4.     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  5.     xmlns:local="using:AppBarControl"  
  6.     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
  7.     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
  8.     mc:Ignorable="d">  
  9.     <Page.BottomAppBar>  
  10.         <CommandBar Height="78" VerticalAlignment="Stretch">  
  11.             <CommandBar.Content>  
  12.                 <Grid/>  
  13.             </CommandBar.Content>  
  14.             <AppBarButton x:Name="AppHome" Icon="Home" Label="Home" Click="AppHome_Click"/>  
  15.             <AppBarButton x:Name="AppContact" Icon="Contact" Label="Contact" Click="AppContact_Click">  
  16.               
  17.             </AppBarButton>  
  18.              
  19.             <AppBarButton x:Name="AppCancel" HorizontalAlignment="Stretch" Icon="Cancel" Label="Cancel" Margin="0,4,0,0" Width="68" Height="45" Click="AppCancel_Click"/>  
  20.         </CommandBar>  
  21.     </Page.BottomAppBar>  
  22.   
  23.     <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">  
  24.         <StackPanel>  
  25.             <TextBlock Text="Home" Padding="25 25 25 25"></TextBlock>  
  26.         </StackPanel>  
  27.     </Grid>  
  28. </Page> 
Step 6- Add the Coding

Double click on the Home button and add the code, given below. Likewise, add the coding (given below) to the contact button and cancel button.

  1. private void AppHome_Click(object sender, RoutedEventArgs e)  
  2.         {  
  3.             Frame.Navigate(typeof(MainPage));  
  4.         }  
  5.   
  6.         private void AppContact_Click(object sender, RoutedEventArgs e)  
  7.         {  
  8.             Frame.Navigate(typeof(Contact));  
  9.         }  
  10.   
  11.            
  12.   
  13.         private void AppCancel_Click(object sender, RoutedEventArgs e)  
  14.         {  
  15.             CommandBar cb = sender as CommandBar;  
  16.             if (cb != null) cb.Background.Opacity = 0.5;  
  17.         } 
Step 7- Run the Application

Now, we are ready to run our project. Thus, click Local Machine to run the Application.



Output

 

 

Conclusion

I hope you understood App Bar control and Command Bar control in Universal Window and how to run it.


Similar Articles