MOBILE

Adding an Application Bar to a Windows Phone 7 Application (part 3) - Adding an Application Bar Using Managed Code

1/30/2011 10:01:19 AM

5. Adding an Application Bar Using Managed Code

The second way to create an Application Bar for a Windows Phone 7 application is to use one of the .NET languages. At the time of this writing, only C# is supported, but plans to support other .Net languages are certainly in the works. Perhaps when you read this, it may even be possible to write applications for Windows Phone 7 with F#!

The steps necessary to create an Application Bar using C# are described here. But first, be sure to remove all of the Application Bar XAML code you wrote for the previous walkthroughs.

  1. You will be editing the MainPage code of your application. To accomplish that, locate the MainPage.xaml.cs file by expanding the MainPage.xaml file in the Solution Explorer. Right-click MainPage.xaml.cs and select View Code.

  2. For easier reference to an Application Bar component inside the Microsoft.Phone assembly (i.e., to avoid typing Microsoft.Phone.Shell.ApplicationBar before each component name), add the following using directive to the top of the MainPage.xaml.cs file:

    using Microsoft.Phone.Shell;

  3. Inside the constructor for the page (i.e., inside the public MainPage() code block), right after InitializeComponent(), initialize the Application Bar and set its IsVisible and IsMenuEnabled properties, as shown in the following code:

    ApplicationBar = new ApplicationBar();
    ApplicationBar.IsVisible = true;
    ApplicationBar.IsMenuEnabled = true;

  4. Initialize Application Bar buttons, providing the relative URI to the image that will be used for each button. Note that you must set the Text property of each button—otherwise you will cause an exception.

    ApplicationBarIconButton btnAdd = new ApplicationBarIconButton(new
    Uri("/Images/appbar.add.rest.png", UriKind.Relative));
    btnAdd.Text = "add";
    ApplicationBarIconButton btnSave = new ApplicationBarIconButton(new
    Uri("/Images/appbar.save.rest.png", UriKind.Relative));
    btnSave.Text = "save";
    ApplicationBarIconButton btnDelete = new ApplicationBarIconButton(new
    Uri("/Images/appbar.delete.rest.png", UriKind.Relative));
    btnDelete.Text = "delete";

  5. Add the buttons to the Application Bar via the following code:

    ApplicationBar.Buttons.Add(btnAdd);
    ApplicationBar.Buttons.Add(btnSave);
    ApplicationBar.Buttons.Add(btnDelete);

  6. Next, we will create two menu items that will appear as text when the ellipsis button is clicked next to the icons on the Application Bar. Very similar to adding icons, there are initialization and addition steps for each menu item. The initialization code for the menu items looks like this:

    ApplicationBarMenuItem menuItem1 = new ApplicationBarMenuItem("Menu Item 1");
    ApplicationBarMenuItem menuItem2 = new ApplicationBarMenuItem("Menu Item 2");


The strings "Menu Item 1" and "Menu Item 2" are the text for the two menu items; in your application, you will certainly change that text to something much more meaningful and fun.

  1. Add menu items to the Application Bar.

    ApplicationBar.MenuItems.Add(menuItem1);
    ApplicationBar.MenuItems.Add(menuItem2);

  2. Finally, you are ready to test the Application Bar. Save your work and press F5 to start debugging the application using Windows Phone 7 emulator.. If you click the ellipsis to the right of the icons, the Application Bar slides up, revealing two menu items, identical to Figure 1.

The full code listing for adding the Application Bar using managed C# code appears here. The full MainPage() constructor listing is included for readability purposes.

Listing 2. C# Code to Implement an Application Bar
public MainPage()
{
InitializeComponent();
SupportedOrientations = SupportedPageOrientation.Portrait |
SupportedPageOrientation.Landscape;

ApplicationBar = new ApplicationBar();
ApplicationBar.IsVisible = true;
ApplicationBar.IsMenuEnabled = true;

ApplicationBarIconButton btnAdd = new ApplicationBarIconButton(new
Uri("/Images/appbar.add.rest.png", UriKind.Relative));
btnAdd.Text = "add";
ApplicationBarIconButton btnSave = new ApplicationBarIconButton(new

Uri("/Images/appbar.save.rest.png", UriKind.Relative));
btnSave.Text = "save";
ApplicationBarIconButton btnDelete = new ApplicationBarIconButton(new
Uri("/Images/appbar.delete.rest.png", UriKind.Relative));
btnDelete.Text = "delete";

ApplicationBarMenuItem menuItem1 = new ApplicationBarMenuItem("Menu Item 1");
ApplicationBarMenuItem menuItem2 = new ApplicationBarMenuItem("Menu Item 2");

ApplicationBar.Buttons.Add(btnAdd);
ApplicationBar.Buttons.Add(btnSave);
ApplicationBar.Buttons.Add(btnDelete);

ApplicationBar.MenuItems.Add(menuItem1);
ApplicationBar.MenuItems.Add(menuItem2);

}

While adding the Application Bar to Windows Phone 7 is cool in itself, we cannot do much with that Application Bar right now. We can push buttons a few hundred times, but nothing changes on the phone screen or inside the application. To react to button press events, we need to write some managed (C# in our case) code, also called the event handler code. In the next section, you'll learn how to write code that processes and reacts to the button press events.

Other  
  •  Adding an Application Bar to a Windows Phone 7 Application (part 2) - Adding a Local Application Bar Using XAML & Adding Menu Items
  •  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
  •  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
  •  
    Top 10
    Exchange Server 2007 : Backup and Recover Data (part 2) - Backup and Recovery with Server 2008
    iPhone 3D Programming : Adding Textures to ModelViewer (part 2) - Generating Texture Coordinates
    Managing the Cache
    JavaScript Patterns : Conventions
    Configuring Local Area Connections
    Installing Windows Server 2008 R2 and Server Core : Upgrading to Windows Server 2008 R2
    WCF Services : Generics
    Windows 7 :Navigating Your Computer with the Address Bar (part 1) - Accessing Locations on Your Computer
    Windows Server 2008 : Domain Name System and IPv6 - Understanding the Evolution of Microsoft DNS
    Excel Programmer : Change Recorded Code
    Most View
    SQL Server 2008 : Developing with LINQ to SQL (part 1)
    Windows Vista : Performing Local PC Administration (part 2) - Performing common workstation administration tasks
    Production Diagnostics Improvements in CLR 4
    Algorithms for Compiler Design: OBTAINING THE REGULAR EXPRESSION FROM THE FINITE AUTOMATA
    Building and Deploying Applications for Windows Azure : Activating the Storage Account Account
    iPhone 3D Programming : Anti-Aliasing Tricks with Offscreen FBOs (part 2) - Jittering
    Windows 7 : Preparing Disks for Use (part 3)
    iPhone 3D Programming : Textures and Image Capture - Texture Coordinates Revisited
    Sharepoint 2010 : Managing SharePoint Content Databases
    Infrastructure Security: The Application Level
    Optimizing for Vertical Search : Optimizing for Product Search
    iPhone 3D Programming : Textures and Image Capture - Dealing with Size Constraints
    The Art of SEO : How Links Influence Search Engine Rankings (part 2) - Additional Factors That Influence Link Value
    Migrating from Legacy SharePoint to SharePoint Server 2010 : Formulating a Migration Strategy
    Getting the Most Out of the Microsoft Outlook Client : Security Enhancements in Outlook 2007
    BizTalk 2006 : Implementing Dynamic Parallel Orchestrations
    Working with Device in Vista
    SQL Azure: Building a Shard (part 1) - Designing the Shard Library Object
    Understanding the Role of ASP.NET View State
    SharePoint 2010 :Implementing a Partner Extranet Solution (part 2) - Configuring Authentication Providers