WP7 Multilingual AppBar.

The current phone app I am working on is based around a pivot and to get the most out of the pivot I want to use a different appbar for each pivot item. Luckily, Microsoft has a good article on how to do it.

How to use different app bars in a single Pivot control for Windows Phone

Unfortunately, the article has a few problems. The main ones deal with a need to know. This is a pivot and this article suggests placing the appbar in App.xaml. This is a pivot and only the page with the pivot needs to know about it or cares. This also means the background code will be in App.xaml.cs which complicates things. Since most of my apps have been multilingual, the solution did not handle the binding required for the resource file.

So the solution was to take the idea and move it to the page that had the pivot. This meant changing the xaml code from using Application.Resource to phone:PhoneApplicationPage.Resources and use throwaway values for the text. The SelectionChanged method of the pivot would take care of adding the correct text. Since I like to have optional menu items and there is no way to hide menu items, I left that up to the code behind and removed any references to the appbar menu from the xaml code. The resulting SelectionChanged method looked like.

private void Pivot_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
switch (((Pivot)sender).SelectedIndex)
{
case 1:
ApplicationBar = ((ApplicationBar)Resources["AppBar1"]);
((ApplicationBarIconButton)ApplicationBar.Buttons[0]).Text = AppResources.AppBar1a;
((ApplicationBarIconButton)ApplicationBar.Buttons[1]).Text = AppResources.AppBar1b;
((ApplicationBarIconButton)ApplicationBar.Buttons[2]).Text = AppResources.AppBar1c;
((ApplicationBarIconButton)ApplicationBar.Buttons[3]).Text = AppResources.AppBar1d;
break;
case 2:
ApplicationBar = ((ApplicationBar)Resources["AppBar2"]);
((ApplicationBarIconButton)ApplicationBar.Buttons[0]).Text = AppResources.AppBar2a;
((ApplicationBarIconButton)ApplicationBar.Buttons[1]).Text = AppResources.AppBar2b;
((ApplicationBarIconButton)ApplicationBar.Buttons[2]).Text = AppResources.AppBar2c;
break;
default:
ApplicationBar = ((ApplicationBar)Resources["AppBar0"]);
((ApplicationBarIconButton)ApplicationBar.Buttons[0]).Text = AppResources.AppBar0a;
((ApplicationBarIconButton)ApplicationBar.Buttons[1]).Text = AppResources.AppBar0b;
break;
}
ApplicationBar.MenuItems.Clear();
ApplicationBar.IsMenuEnabled = true;

ApplicationBarMenuItem appBarMenuItem;
appBarMenuItem = new ApplicationBarMenuItem(AppResources.lblLive);
appBarMenuItem.Click += new EventHandler(Click_Live);
ApplicationBar.MenuItems.Add(appBarMenuItem);
if (App.isTrial)
{
appBarMenuItem = new ApplicationBarMenuItem(AppResources.lblBuyNow);
appBarMenuItem.Click += new EventHandler(Click_Buy);
ApplicationBar.MenuItems.Add(appBarMenuItem);
}
if (App.EdomDog)
{
appBarMenuItem = new ApplicationBarMenuItem(AppResources.lblEdomDog);
appBarMenuItem.Click += new EventHandler(Click_EdomDog);
ApplicationBar.MenuItems.Add(appBarMenuItem);
}
}

Now, as the app evolves, I have an easy framework to build the appbars on. The code is contained to the page that needs it, it supports multiple languages and it supports optional items.

John Marshall… Visio MVP       Visio.MVPs.org

Published by johnvisiomvp

The original Visio MVP. I have worked with the Visio team since 1993

One thought on “WP7 Multilingual AppBar.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s