Global Application Bar in Windows Phone 7

In a Windows Phone 7 application, Application Bar is very useful. With as assumption that, you know what is Application Bar? I will discuss with you

  • How Application Bar can be used as static resource?
  • How to make Application Bar as global resource?
  • How to use same Application Bar in multiple pages?

Designing Global Application Bar

To use Application Bar globally in the application, you need to make it as Static Resource. For that open App.xaml and add below code in the resource section,


<Application.Resources>
<shell:ApplicationBar x:Key="MyGlobalAppBar" IsVisible="True" IsMenuEnabled="True">
<shell:ApplicationBarIconButton IconUri="/button1.png" Text="FIRST BUTTON" Click="ApplicationBarIconButton_Click" />
<shell:ApplicationBarIconButton IconUri="/button2.png" Text="SECOND BUTTON" Click="ApplicationBarIconButton_Click_1" />
<shell:ApplicationBar.MenuItems>
<shell:ApplicationBarMenuItem Text="First Menu Item" Click="MenuItem1_Click" />
<shell:ApplicationBarMenuItem Text="Second Meny Item" Click="MenuItem2_Click" />
</shell:ApplicationBar.MenuItems>
</shell:ApplicationBar>
</Application.Resources>

There are few points worth discussing

  • I have added as resource two images and setting them as image of the button
  • There are two buttons and two menu items. You are free to change this as of your requirement.
  • Button and menu item click event is being handled on the app.xaml.cs file
  • Key of the global application bar is MyGlobalAppBar . You need to use this key at time of using this application bar. However you are free to give different key and use them accordingly

Code behind for Global Application Bar

In App.Xaml.cs file you need to handle the click event of buttons and menu item as below,


private void ApplicationBarIconButton_Click(object sender, EventArgs e)
{
MessageBox.Show("I am Button 1");
}
private void ApplicationBarIconButton_Click_1(object sender, EventArgs e)
{
MessageBox.Show("I am Button 2");
}
private void MenuItem1_Click(object sender, EventArgs e)
{
MessageBox.Show("I am Menu Item 1");
}
private void MenuItem2_Click(object sender, EventArgs e)
{
MessageBox.Show("I am Menu Item 2");
}

&nbsp;

Creating page to use Global Application Bar

You need to set the application bar in each page as below. If you remember MyGlobalAppBar is key of the application bar we created as static resource.

clip_image001

Now when you run the application, you should get below Application Bar on each page you have set it .

image

I hope this post was useful. Thanks for reading and tune in for the next post  Smile

If you find my posts useful you may like to follow me on twitter http://twitter.com/debug_mode or may like Facebook page of my blog http://www.facebook.com/DebugMode.Net If you want to see post on a particular topic please do write on FB page or tweet me about that, I would love to help you.

4 thoughts on “Global Application Bar in Windows Phone 7

  1. Thanks for this post…. looks like we all need to brush up our Visual Studio skills:) The Global Appllication Bar is an ingenious way to give uniformity and functionality to the Windows Phone platform….I personally think the Bar is discreet enough to let the App / Screen shine without compromising on powerful features….

    Microsoft was smart in allowing Devs to target the Bar to specify commands relative to the App they create….
    Windows Phone rocks!!!! THanks Microsoft:) We love you guys

Leave a comment