Coding against the application bar in Windows Phone 7

If you are coding Silverlight apps for Windows Phone 7 you have probably used the ApplicationBar for your menu needs. If it’s confusing you with the way it works, or even giving you NullPtr exceptions when you try to access it’s properties, it’s because it’s no Silverlight control.

Looking at the XAML for the template “Windows Phone Application” in Visual Studio, there is an ApplicationBar defined. If you have used that template and tried databinding the ApplicationBar then you will have noticed that it’s not working, or even producing exceptions.

If you need to control the ApplicationBar in any way and change things dynamically, you need to do this from the c# codebehind. Even if you name the bar (or it’s components) with x:Name in XAML, you won’t be able to reach those objects from the codebehind.

What you need to do is to work directly with the instanced ApplicationBar from the codebehind. Say for example you need to change the icon for button number 3 on the fly, this is the way to do it:

((ApplicationBarIconButton)(ApplicationBar.Buttons[2])).IconUri =
    new Uri("NewIcon.png", UriKind.Relative);

This is also very important to remember if you are going to localize your app, as you can’t databind the translations as you can with all other Silverlight controls!

ApplicationBar – not your usual control
Tagged on:                     

One thought on “ApplicationBar – not your usual control

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.