Life with Visio and other Microsoft Toys!

WP7 Multilingual AppBar.

with one comment

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;
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;
ApplicationBar = ((ApplicationBar)Resources["AppBar0"]);
((ApplicationBarIconButton)ApplicationBar.Buttons[0]).Text = AppResources.AppBar0a;
((ApplicationBarIconButton)ApplicationBar.Buttons[1]).Text = AppResources.AppBar0b;
ApplicationBar.IsMenuEnabled = true;

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

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


Written by johnvisiomvp

January 15, 2014 at 9:42 am

One Response

Subscribe to comments with RSS.

  1. […] /* 0) { ratingctl00_cphMiddle_cphContent_tr3w45fwwvre_itemRating = result['Rating']; SetCurrentRating('ctl00_cphMiddle_cphContent_tr3w45fwwvre_itemRating_pnlHolder', result['Rating'], "disabled fullStar", "disabled emptyStar", true); if(votesCountctl00_cphMiddle_cphContent_tr3w45fwwvre_itemRating!=null) { votesCountctl00_cphMiddle_cphContent_tr3w45fwwvre_itemRating ++; SetVotesCount('ctl00_cphMiddle_cphContent_tr3w45fwwvre_itemRating_lblUsersRated', '(' + votesCountctl00_cphMiddle_cphContent_tr3w45fwwvre_itemRating + ' votes)'); } SetRatingCookie('r', 'i35060', '1'); } else if (result['Status'] == 1) { alert('The session has expired. Please refresh the page to be able to vote!'); } } /* ]]> */ (0 votes) 0 comments   /   posted by Silverlight Show on Jan 17, 2014 Tags:   windows-phone , wpdev , john-marshall Read original post at John Marshall's Blog […]

Leave a Reply

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

You are commenting using your 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

%d bloggers like this: