For those who still haven’t got frustrated about the ApplicationBar let me start by opening your eyes with this statement :
The ApplicationBar is really weird and sorry to say this: Stinks, no Binding is supported, no commands etc..
With Ninja developer reflexes you wonder on and just convince yourself: It’s ok I’ll just create my own ApplicationBar since IApplicationBar interface is there (the ApplicationBar class is sealed so you can’t extend from it)
So you start off developing your own class that implements from IApplicationBar, and you’re all happy and can’t wait till you’ve finished your own custom ApplicationBar. Oh but wait waiiiiittt…you thought this was it??
So you’re there now on your View or whatever trying to add your custom made ultimate ninja ApplicationBar, and what’s that? your code is highlighted and you have a message that says : PhoneApplicationPage only supports the “ApplicationBar” implementation of “IApplicationBar” (even though the PhoneApplicationPage’s ApplicationBar property is an IApplicationBar)
So right now feeling disgusted, and wanting some answers you bother a couple of intellects you might know if they have an answer for that?..You quickly find yourself very limited in choices.
Unless you like copy / pasting a lot everywhere; a semi less dirty/tiring way to figure this out is to use extension methods.
So let me be clearer with an example; Not so long ago I had to change many different application bars a bit everywhere in my application, but the change had to be done from my App.xaml.cs because that’s where I received my notifications.
Now in some cases you could just have all your ApplicationBar as a StaticResource in your App.xaml or a resource dictionnary (but that wasn’t my case).
So as a fan of the MVVM Light framework by Laurent BugnionI used the Messenger so I can send messages from my “App” object to my application bars.
Here are the extension methods:
public static void RegisterMessenger(this ApplicationBar appbar)
{
Messenger.Default.Register<IconCount>(appbar, (iconCount) =>
{
if (appbar.Buttons != null)
{
if (appbar.Buttons.Count > iconCount.IconIndex && appbar.Buttons[iconCount.IconIndex] != null)
{
var button = appbar.Buttons[iconCount.IconIndex] as ApplicationBarIconButton;
//TODO faire les modifications voulues ICI
}
}
});
}
public static void UnRegisterMessenger(this ApplicationBar appbar)
{
Messenger.Default.Unregister<IconCount>(appbar);
}
IconCount here is a simple structure that simplifies the message I’m sending, you can use any type you want like String etc..It all depends on your needs.
public struct IconCount
{
public int IconIndex, Count;
}
While we’re at it, we’ll add localization extension methods to (in case you have a multi-lingual application)
public static void LocalizeAppBar(this ApplicationBar appbar)
{
//localize icon button texts
if (appbar.Buttons != null)
{
foreach (ApplicationBarIconButton btn in appbar.Buttons)
{
switch (btn.Text)
{
case "Profile":
btn.Text = LocalizedResources.APPBAR_PROFILE;
break;
case "Add":
btn.Text = LocalizedResources.APPBAR_ADD;
break;
case "Settings":
btn.Text = LocalizedResources.APPBAR_SETTINGS;
break;
case "Delete":
btn.Text = LocalizedResources.APPBAR_DELETE;
break;
case "Save":
btn.Text = LocalizedResources.APPBAR_SAVE;
break;
default:
break;
}
}
}
//localize menu buttons texts
if (appbar.MenuItems != null)
{
foreach (ApplicationBarMenuItem itm in appbar.MenuItems)
{
switch (itm.Text)
{
case "Profile":
itm.Text = LocalizedResources.APPBAR_PROFILE;
break;
default:
break;
}
}
}
}
}
Now in views, register your ApplicationBar and localize it:
if (this.ApplicationBar != null)
{
((ApplicationBar)this.ApplicationBar).RegisterMessenger();
((ApplicationBar)this.ApplicationBar).LocalizeAppBar();
}
Now that you’re ApplicationBars are ready to receive the messages; you can send a message with the Messenger this way:
int countNewMessage = conv.Where(c => c.HasNewMessage).Count();
Messenger.Default.Send<IconCount, ApplicationBar>(new Lib.IconCount()
{
Count = countNewMessage,
IconIndex = 1,
});
Again instead of an IconCount you can just send a string etc(an example is provided below)..The second parameter is the target of the message, so if the message is destined to everyone that’s subscribed to receive the message of the type IconCount I’d just take off ApplicationBar and put nothing.
So here’s a simpler example of the Messenger:
In order to send a message of type string to all the objects that are subscribed to receive string messages:
Messenger.Default.Send<string>("Hello");
In order to receive that message on a particular object:
Messenger.Default.Register<string>(this,(msg)=>
{
//msg contains the string you sent
//Code you want to run after the message is received
});
MVVM Light (which works on SL/WPF and WP7) comes with a bunch of cool things like MVVM templates for your projects, RelayCommand for SL3/WP7 and a cool trigger that I like “EventToCommand” etc..so to discover more just check out this page.
So hope these cool tips would be helpful in your ongoing ApplicationBar battle.

