Adding an Application Bar to a Windows Phone 7 Application (part 1) - Adding Images for Use with Application Bar Buttons & Adding a Global Application Bar Using XAML

1/30/2011 9:58:27 AM
Windows Phone 7 provides two types of Application Bar for use with phones apps: a global bar and a local bar. The global Application Bar must be defined in App.xaml, and it can be added to any page within the Windows Phone 7 application with a single line of XAML code. There are two ways to define a local Application Bar and add it to a particular application page:
  1. Using XAML

  2. Using Managed Code (i.e., C#)

You'll get to try both methods in this article, where you'll learn to build an Application Bar that provides simple functionality and asks for a person's name, then acts like that name has been saved to the database or the cloud storage. Regardless of the approach you choose and regardless of whether you are building a local or a global Application Bar, there is a preparatory step you should take before you can properly display and use it. That step involves adding images for your Application Bar buttons to project resources.

Figure 1. Application Bar with menu items shown. Note how menu items appear in lower-case, regardless of the letter casing when they were created.

1. Adding Images for Use with Application Bar Buttons

Because the maximum size of each Application Bar icon is 48x48 pixels, the size of each icon you add is limited to 26x26 pixels so that a circle can be properly drawn around it. Since Windows Phone 7 supports the concept of themes, the background of an icon has to match the rest of the theme, and therefore should be made transparent. On this transparent background, the actual graphic should have white foreground color using an alpha channel. Fortunately, in many cases, you won't have to create icons yourself, since Microsoft has released a set of commonly used images for Windows Phone 7, all properly sized and formatted in Microsoft's approved style. You can grab a copy of those icons here:

Like everything else on the Internet, the aforementioned URL is subject to change. If, for some reason, you are not able to download icons by using that link, simply go to and search for "Application Bar Icons for Windows Phone 7 Series."

  1. Once you've downloaded the small 260KB ZIP file containing the images, unzip them to an easy-to-access location on your computer. Follow the next steps to properly include those images into the project. Start off by creating a new Visual Studio Project and naming it ApplicationBarSample.

  2. Next, let's organize the project for easier readability by creating a folder for the icon images you'll use in the ApplicationBarSample project. Right-click the project name in the Solution Explorer, select Add=>New Folder. Name the folder "Images."

  3. Next, copy the downloaded icon images to the newly created folder within your project. Using Windows Explorer, go to the folder to which you downloaded the zipped icons file and copy the image files you need from there to the Images folder of your project. In the example that follows, you will be using the images located in the WP7AppBarIcons\_basic_shellcommon\dark subfolder of the downloaded archive. Make sure to copy the *.png files only, without any folder structure.

  4. Now the images are copied, but the Visual Studio still needs to make them a part of the project. Right-click Solution Explorer, then select Add=>Existing Item. Select all images by clicking each one while holding the Ctrl key down, or (quicker) by clicking the first image, holding down the Shift key, and then clicking the last image in the set.

  5. Finally, you need to instruct Visual Studio to include new images in every build. For each image, right-click the image in the Solution Explorer and choose Properties (you can also press F4 to bring up the Properties dialog). In the Properties dialog box, set the Build action to "Content" and set the Copy to Output property to "Copy Always," as shown in Figure 2.

    Figure 2. For each image, make sure to set Build Action to "Content" and Copy to Output Directory to "Copy Always".

Now that the project knows where to find the icon images for an Application Bar, it's time to create a project to showcase the Application Bar's features.

2. Adding a Global Application Bar Using XAML

A global Application Bar is created as an application resource in the app.xaml section. Follow these steps to create and add a global Application Bar.

  1. In Solution Explorer, right-click the App.xaml file for the ApplicationBarSample project and select Open. This action causes Visual Studio to display the XAML code for the application's resource and configuration page.

  2. Next, you need to paste the complete XAML definition of the Application Bar with three icons and two menu items in the Application Resources section. Locate the <Application.Resources> section of the App.xaml and paste the following code within that section. Note that setting the Text property for each control is required:

    <shell:ApplicationBar x:Key="GlobalAppMenuBar" Opacity="1" IsVisible="True" IsMenuEnabled="True">
    <shell:ApplicationBarIconButton IconUri="/Images/"
    <shell:ApplicationBarIconButton IconUri="/Images/"
    <shell:ApplicationBarIconButton IconUri="/Images/"
    <shell:ApplicationBarMenuItem Text="Menu Item 1" IsEnabled="True">
    <shell:ApplicationBarMenuItem Text="Menu Item 2" IsEnabled="True">

  3. With the global Application Bar defined, you are ready to add it to the pages within our application. Open MainPage.xaml and add the following attribute within the <phone:PhoneApplicationPage> node:

    ApplicationBar="{StaticResource GlobalAppMenuBar}"

  4. Press F5 to run the application. You should see an Application Bar identical to the one shown in Figure 7-2.

Before moving onto the next section and taking a look at a local Application Bar, let's clean up the MainPage.xaml code by removing the ApplicationBar="{StaticResource GlobalAppMenuBar}" XAML. If we don't do that, we will get an application exception after we add a local Application Bar.

  •  iPhone Application Development : Creating and Managing Image Animations and Sliders (part 3) - Finishing the Interface
  •  iPhone Application Development : Creating and Managing Image Animations and Sliders (part 2) - Adding an Image View
  •  iPhone Application Development : Creating and Managing Image Animations and Sliders (part 1)
  •  iPhone Application Development : User Input and Output
  •  Windows Phone 7 : Using Accelerometer Data to Move a Ball
  •  Server-Side Browser Detection and Content Delivery : Mobile Detection (part 4) - Device Libraries
  •  Server-Side Browser Detection and Content Delivery : Mobile Detection (part 3) - Transcoders
  •  Server-Side Browser Detection and Content Delivery : Mobile Detection (part 2) - Detecting the Context
  •  Server-Side Browser Detection and Content Delivery : Mobile Detection (part 1) - HTTP
  •  Using Windows Phone 7 Technologies : Retrieving Accelerometer Data (part 2)
  •  Using Windows Phone 7 Technologies : Retrieving Accelerometer Data (part 1)
  •  Using Windows Phone 7 Technologies : Understanding Orientation and Movement
  •  Programming the Mobile Web : HTML 5 (part 4) - Client Storage
  •  Programming the Mobile Web : HTML 5 (part 3) - Offline Operation
  •  Programming the Mobile Web : HTML 5 (part 2) - The canvas Element
  •  Programming the Mobile Web : HTML 5 (part 1)
  •  Windows Phone 7 : Submitting Your First Windows Phone Application to the Windows Phone Marketplace
  •  Windows Phone 7 : Packaging, Publishing, and Managing Applications
  •  Mobile Application Security : Windows Mobile Security - Development and Security Testing (part 3)
  •  Mobile Application Security : Windows Mobile Security - Development and Security Testing (part 2)
    Top 10
    Windows Phone 7 Advanced Programming Model : Advanced Data Binding (part 2) - Syndicated Services
    Algorithms for Compiler Design: STACK ALLOCATION
    SQL Server 2008 : Audit-Related Startup and Shutdown Problems
    Cloud Application Architectures : Machine Image Design
    Cloud Application Architectures : Web Application Design
    Getting Started with Device Manager
    Installing Exchange Server 2010 : Unattended setup
    Introduction to Transport-Level Security in Windows Server 2008 R2
    Programming Code-Access Security
    Most View
    Deploying the Client for Microsoft Exchange Server 2010 : Preparing the Deployment
    Silverlight : Build a Download and Playback Progress Bar
    Advanced ASP.NET : Understanding Caching
    Building Android Apps : Installing KiloGap in the Emulator
    Getting the Most Out of the Microsoft Outlook Client : Using Outlook 2007 (part 3) - Using Group Schedules
    Implementing Client Access and Hub Transport Servers : Installing the Client Access Server
    Microsoft XNA Game Studio 3.0 : Displaying Images - Using Resources in a Game (part 4) - Filling the Screen
    iPhone Programming : Table-View-Based Applications - Building a Model
    Logon Authentication in Windows Vista
    Windows Server 2008 : Understanding Active Directory Sites (part 2)
    Upgrading to Windows Server 2003 : Planning a Windows NT Domain Upgrade (part 2) - Planning the Active Directory Forest
    Combinations and Permutations with F#
    Sharing Files and Folders Over the Network in Vista
    SQL Server 2005 : Exception Handling
    SharePoint Administration with PowerShell (part 2)
    SQL Server 2008 : Using Remote Stored Procedures
    Windows 7 : Managing and Applying LGPOs (part 1) - Configuring Local Security Policies
    Windows Phone 7 Development : Building a Phone Client to Access a Cloud Service (part 3) - Coding the BoolToVisibilityConvert
    SQL Azure : Managing a Shard (part 1) - Managing Exceptions & Managing Performance
    IIS 7.0 : Configuration File Hierarchy