Application Bar Buttons Are Null

Windows Phone From Scratch #50

In a recent posting, I demonstrated how easy it is to add buttons to the phone’s Application Bar.

I went back to that application to disable the “faster” button when the maximum speed was reached. I named the button and then tried to access that button in code. Much to my surprise, the button was null. It was always null.

I’ve recently been turning to search rather than to the standard documentation as my first line of defense when I run into unexpected problems.  Thus, I entered “Windows Phone Application Bar Button Null” into Bing and the first result was a wonderful article on this very issue written by Laurent Bugnion (author of MVVM Light). 

He solves the problem elegantly, and I won’t repeat his work here, but I will walk you through the changes I made to the program.

I first opened the program and, following Laurent’s suggestion, I created an enumeration for the two buttons that were in place,

public enum ButtonTypes
{
   Slower = 0,
   Faster = 1
}

 

I created a method that retrieves the button from the ApplicationBar’s Buttons property, using an index and casting the result to be of type ApplicationBarIconButton.

 public ApplicationBarIconButton GetButton( 
       ButtonTypes whichButton )
 {
    return ApplicationBar.Buttons[(int) whichButton] 
       as ApplicationBarIconButton;
 }

Having retrieved the button, I could now set it to disabled if conditions were correct

if ( currentTS <= new TimeSpan( 0, 0, 0, 0, INCREMENT ) )
{
   GetButton( ButtonTypes.Faster ).IsEnabled = false;
}
else
{
   GetButton( ButtonTypes.Faster ).IsEnabled = true;
}

Problem solved, and quickly, thanks to Bing and Laurent. 

About Jesse Liberty

Jesse Liberty has three decades of experience writing and delivering software projects and is the author of 2 dozen books and a couple dozen online courses. His latest book, Building APIs with .NET will be released early in 2025. Liberty is a Senior SW Engineer for CNH and he was a Senior Technical Evangelist for Microsoft, a Distinguished Software Engineer for AT&T, a VP for Information Services for Citibank and a Software Architect for PBS. He is a Microsoft MVP.
This entry was posted in Essentials, Mini-Tutorial, WindowsPhone and tagged , . Bookmark the permalink.

4 Responses to Application Bar Buttons Are Null

  1. Brent says:

    Thanks for taking the time to post it! Appreciate it.

  2. James says:

    Use “sender as” is a alternative way to get instance, ex:
    (sender as ApplicationBarIconButton).IsEnabled = false;

  3. I ended up doing the same as Tim above – in all my PhoneApplicationPage code-behinds, I call a WireApplicationBarItems method where I wire things up manually. I also have a ViewModel properties for those appbar items, and a property changed event handler where I catch those changes and update the appbar items.

    You’d think that it’d be pretty easy to auto-generate the appbar item wiring code, so I’m scratching my head as to why they didn’t.

  4. There is an easier way to create and wire up application bar button that does not require a method. Use the x:Name attribute in xaml, and they will have fields created for them.

    You still need to wire the buttons by hand in the constructor. After the InitializeComponents call, add the following:

    button1 = (ApplicationBarIconButton)ApplicationBar.Buttons[0];

Comments are closed.