AppBarButton Control in Universal Apps

This article demonstrates how to use the AppBarButton control in a Universal App.

An AppBarButton control is used where we request users to swipe menus and click in a Universal app.



There are different types of menuitem icons: Symbol, Bitmap(picture), Font and Path.
 
The following code snippet demonstrates how to set these different types of menu items.
 
The CommandBar control can have a collection of AppBarButton controls. The BitmapIcon, FontIcon, and PathIcon attributes are used to set the values.
  1. <Page.BottomAppBar>    
  2.        <CommandBar>    
  3.            <AppBarButton Icon="Like" Label="SymbolIcon" />    
  4.    
  5.            <!-- App bar button with bitmap icon. -->    
  6.            <AppBarButton Label="BitmapIcon" >    
  7.                <AppBarButton.Icon>    
  8.                    <BitmapIcon UriSource="ms-appx:///Assets/StoreLogo.png"/>    
  9.                </AppBarButton.Icon>    
  10.            </AppBarButton>    
  11.    
  12.            <!-- App bar button with font icon. -->    
  13.            <AppBarButton Label="FontIcon" >    
  14.                <AppBarButton.Icon>    
  15.                    <FontIcon FontFamily="Candara" Glyph="Σ"/>    
  16.                </AppBarButton.Icon>    
  17.            </AppBarButton>    
  18.    
  19.            <!-- App bar button with path icon. -->    
  20.            <AppBarButton Label="PathIcon" >    
  21.                <AppBarButton.Icon>    
  22.                    <PathIcon Data="F1 M 16,12 20,2L 20,16 1,16" HorizontalAlignment="Center"/>    
  23.                </AppBarButton.Icon>    
  24.            </AppBarButton>    
  25.        </CommandBar>                 
  26.    </Page.BottomAppBar>   
When you try the above code, it shall show you the AppBarButtons at the bottom of the AppBar. 

 

AppBarButton is useful, if you're planning on giving user extra settings, social media links that look like anti-UX when put in the UI screen.

As its derived from the Button class, the AppBarButton control has the Click event.Lets make an example how to declare click event.

I'd like to show users a Messagebox once they click the AppBarButton's declared in xaml,but before that give every button a name so that we can access them from code:

  1. <AppBarButton Name="btnSymbol" Icon="Like" Label="SymbolIcon" />  
  2.   
  3. <!-- App bar button with bitmap icon. -->  
  4. <AppBarButton Name="btnBitmap" Label="BitmapIcon" >  
  5.  <AppBarButton.Icon>  
  6.   <BitmapIcon UriSource="ms-appx:///Assets/StoreLogo.png"/>  
  7.  </AppBarButton.Icon>  
  8. </AppBarButton>  
  9.   
  10. <!-- App bar button with font icon. -->  
  11. <AppBarButton Name="btnFont" Label="FontIcon" >  
  12.  <AppBarButton.Icon>  
  13.   <FontIcon FontFamily="Candara" Glyph="Σ"/>  
  14.  </AppBarButton.Icon>  
  15. </AppBarButton>  
  16.   
  17. <!-- App bar button with path icon. -->  
  18. <AppBarButton Name="btnPath" Label="PathIcon" >  
  19.  <AppBarButton.Icon>  
  20.   <PathIcon Data="F1 M 16,12 20,2L 20,16 1,16" HorizontalAlignment="Center"/>  
  21.  </AppBarButton.Icon>  
  22. </AppBarButton>  
Then,open up your MainPage.xaml.cs file,and create a new method to show MessageBox:
  1. public async void ShowMessage(string message)  
  2. {  
  3.    var msg = new Windows.UI.Popups.MessageDialog(message);  
  4.    msg.DefaultCommandIndex = 1;  
  5.    await msg.ShowAsync();  
  6. }  
Finally add these delegates in your constuctor or Page Load event: 
  1. public MainPage()  
  2. {  
  3.   
  4.   this.InitializeComponent();  
  5.   
  6.   btnBitmap.Click += (e, f) => { ShowMessage("Clicked BitMap Button");  };  
  7.   btnFont.Click   += (e, f) => { ShowMessage("Clicked Font Button");    };  
  8.   btnPath.Click   += (e, f) => { ShowMessage("Clicked Path Button"); };  
  9.   btnSymbol.Click += (e, f) => { ShowMessage("Clicked Symbol Button"); };  
  10.   
  11. }  
 
 
 
 
Thats It!