Skip to content

Localize the ApplicationBar

November 2, 2010
Update: Since I wrote this post, I found this method which is better since it supports the binding  : http://blogs.codes-sources.com/nicolas/archive/2010/08/19/wp7-comment-avoir-une-applicationbar-bindable.aspx

One thing I left out of my post about localization is the management of the ApplicationBar in your Windows Phone 7 application. This widget is special because it’s not a Silverlight one so it doesn’t benefit from the usual infrastructure like binding.  The official documentation shows how you can localize the text but if you need dynamic localization it can be daunting. I found no workaround but I share my solution here.
For all sort of purposes (including the support of INotifyPropertyChanged) I have my own base type  for the pages. Inside it I add a virtual method UpdateApplicationBar. This method is  called inside my overridden version of the well-know OnNavigateTo(…). In each page I override the UpdateApplicationBar with specific text to assign each button with some helper/extension methods.
The image:
classdiagramm
In the base type:
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
	base.OnNavigatedTo(e);
	UpdateApplicationBar();
}

protected virtual void UpdateApplicationBar()
{

}

protected void UpdateButton(int index, string text)
{
	ApplicationBar.UpdateTextButton(index, text);
}

protected void UpdateMenuItem(int index, string text)
{
	ApplicationBar.UpdateTextMenuItem(index, text);
}
The extension methods:
 public static class ApplicationBarExtensions
{
	public static void UpdateTextButton(this IApplicationBar appBar, int index, string text)
	{
		var button = appBar.Buttons[index] as ApplicationBarIconButton;
		if(null != button)
		{
			button.Text = text.ToLower();
		}
	}

	public static void UpdateTextMenuItem(this IApplicationBar appBar, int index, string text)
	{
		var menuItem = appBar.MenuItems[index] as ApplicationBarMenuItem;
		if (null != menuItem)
		{
			menuItem.Text = text;
		}
	}

}
In a specific page:
protected override void UpdateApplicationBar()
{
	base.UpdateApplicationBar();
	UpdateButton(0, Labels.LocateList);
	UpdateButton(1, Labels.LocateMap);
	UpdateButton(2, Labels.RefreshMap);
}

An sample:

english
french
Advertisements
4 Comments leave one →
  1. December 14, 2010 6:32 pm

    Hallo,
    thank you for this Post.
    But there is a little bug in your extension method “UpdateTextMenuItem”.
    It must be: appBar.MenuItems[index] as ApplicationBarMenuItem; and not appBar.Buttons[index] as ApplicationBarMenuItem;

    Anyway I came from AS3 Flex/Flash development and I do not understand why this kind of invoking static methods is possible?

    With the Extension Class you extend the “Base” Class static methods, but how this works?

    thank you
    bye
    Markus

    • December 14, 2010 7:30 pm

      Hello Markus,

      Thanks for the comment (and spotting the typo) !

      The “extension methods” allow you to “add” a method to a type you don’t have the control, here IApplicationBar. It’s mostly to replace Helper classes. The only requirements are :

      1. the extension methods must be declared in a static public class, they must be static as well
      2. the first argument is the type you “extend” with the keyword this.

      A very common sample is :

      if(!String.IsNullOrEmpty(mystringvariable) ){}
      to
      if(mystringvariable.HasValue()){} //more readable

      where HasValue is declared as :

      public static bool HasValue(this string value)
      {
      return !String.IsNullOrEmpty(valule);
      }

  2. vlad permalink
    May 12, 2011 4:29 am

    I want to create an Update method in the base class in which i want to iterate through all the buttons and menu items of an appbar

    protected void UpdateApplicationBar()
    {
    if (ApplicationBar == null)
    {
    return;
    }

    int iButtonCount = ApplicationBar.Buttons.Count;

    for (int iter = 0; iter < iButtonCount; iter++ )
    {
    string str = (ApplicationBar.Buttons[iter] as ApplicationBarIconButton).Text;

    }
    }

    The issue is that (ApplicationBar.Buttons[iter] as ApplicationBarIconButton).Text; returns the value of "Labels.LocateMap"

    Is there any way I can look-up into the resource file an get the ID ("LocaleMap") based on the value received from (ApplicationBar.Buttons[iter] as ApplicationBarIconButton).Text

    Thanks,
    Vlad

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

%d bloggers like this: