Export (0) Print
Expand All
Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.
12 out of 14 rated this helpful - Rate this topic

How to create an app bar using code for Windows Phone

April 14, 2014

Applies to: Windows Phone 8 and Windows Phone Silverlight 8.1 | Windows Phone OS 7.1

You can create an Application Bar by using C# or Visual Basic code that provides users with quick access to an application’s most common tasks. To decide whether you should create your Application Bar in XAML or in code, see App bar for Windows Phone.

The following illustration shows an example of an Application Bar, after the user has expanded it.

App Bar expanded with menu items

This topic contains the following sections.

You can add an Application Bar to your application entirely in code, without editing the XAML file. This procedure assumes that you have a Windows Phone application that has a page. Before you can use images for icon buttons on the Application Bar, you must first add them to your application. For more information, see App bar icon buttons for Windows Phone.

To create an Application Bar in code

  1. Open the code-behind file for your page in the editor.

  2. At the top of the code, add the following statement.

    using Microsoft.Phone.Shell;
    
  3. In the constructor for the page, after the call to InitializeComponent, add the following code. This code initializes a new ApplicationBar object and assigns it to the ApplicationBar property of the page.

    ApplicationBar = new ApplicationBar();
    
  4. Set the Application Bar properties as needed. For more information about the properties, see App bar for Windows Phone.

    ApplicationBar.Mode = ApplicationBarMode.Default;
    ApplicationBar.Opacity = 1.0; 
    ApplicationBar.IsVisible = true;
    ApplicationBar.IsMenuEnabled = true;
    
  5. Create one or more ApplicationBarIconButton objects as needed. Set the icon images and button text, and then add them to the Application Bar. If you do not set the button text, a run-time exception occurs.

    ApplicationBarIconButton button1 = new ApplicationBarIconButton();
    button1.IconUri = new Uri("/Images/YourImage.png", UriKind.Relative);
    button1.Text = "button 1";
    ApplicationBar.Buttons.Add(button1);
    
  6. The menu items are optional. Create one or more ApplicationBarMenuItem objects as needed. Set the text, and then add them to the Application Bar.

    ApplicationBarMenuItem menuItem1 = new ApplicationBarMenuItem();
    menuItem1.Text = "menu item 1";
    ApplicationBar.MenuItems.Add(menuItem1);
    
  7. The following is an example of a completed Application Bar. You add click events and handlers in the next procedure.

    public MainPage()
    {
        InitializeComponent();
    
        ApplicationBar = new ApplicationBar();
    
        ApplicationBar.Mode = ApplicationBarMode.Default;
        ApplicationBar.Opacity = 1.0; 
        ApplicationBar.IsVisible = true;
        ApplicationBar.IsMenuEnabled = true;
    
        ApplicationBarIconButton button1 = new ApplicationBarIconButton();
        button1.IconUri = new Uri("/Images/YourImage.png", UriKind.Relative);
        button1.Text = "button 1";
        ApplicationBar.Buttons.Add(button1);
    
        ApplicationBarMenuItem menuItem1 = new ApplicationBarMenuItem();
        menuItem1.Text = "menu item 1";
        ApplicationBar.MenuItems.Add(menuItem1);
    }
    

The icon buttons and menu items expose click events that you can handle in your code.

To handle the click events

  1. Open the code-behind file for your page in the editor.

  2. For each icon button and menu item, identify the event to call when the user clicks. The following is an example of the Application Bar from the previous procedure with the click events identified.

    ApplicationBarIconButton button1 = new ApplicationBarIconButton();
    button1.IconUri = new Uri("/Images/YourImage.png", UriKind.Relative);
    button1.Text = "button 1";
    ApplicationBar.Buttons.Add(button1);
    button1.Click += new EventHandler(button1_Click);
    
    ApplicationBarMenuItem menuItem1 = new ApplicationBarMenuItem();
    menuItem1.Text = "menu item 1";
    ApplicationBar.MenuItems.Add(menuItem1);
    menuItem1.Click += new EventHandler(menuItem1_Click);
    
  3. For each icon button and menu item, add the event to call when the user clicks. Add the code inside the page class. The following is an example of the click events for the Application Bar from the previous procedure.

    private void button1_Click(object sender, EventArgs e)
    {
        MessageBox.Show("Button 1 works!");
        //Do work for your application here.
    }
    
    private void menuItem1_Click(object sender, EventArgs e)
    {
        MessageBox.Show("Menu item 1 works!");
        //Do work for your application here.
    }
    

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.