views:

63

answers:

2

hey,

I'm developing a WP7 app and needs to change the icon of a button on the application bar given the state of a request. I have tried:

if (App.Servers[index].ServerState == "Enabled") { DetailsAppBar.btnStart.IconUri = new Uri("/AppBar/appbar.stop.rest.png"); }

  else
  {
    DetailsAppBar.btnStart.IconUri = new Uri("/AppBar/appbar.transport.play.rest.png");
  }

This doesn't give me an error in the code, but it can't compile.... any hints to do this is appreciated :)

thanks

+3  A: 

ApplicationBar is a special control that does not behave as other Silverlight controls (see Peter Torr's post on the subject). One of the consequences is that names given in XAML to app bar buttons generate fields in code-behind that are always null.

I'm guessing that in your case the btnStart field in DetailsAppBar is set to null, and thus trying to set its IconUri property results in a NullReferenceException being thrown.

To access a button in an application bar, you must instead reference it by its zero-based index in the buttons list. For instance, the code below returns a reference to the third button in the app bar:

button = (IApplicationBarIconButton)ApplicationBar.Buttons[2];
Andréas Saudemont
yeah I just found out :) thanks
Christian M
A: 

figured it out...

((ApplicationBarIconButton)ApplicationBar.Buttons[2]).IconUri = new Uri("/AppBar/appbar.stop.rest.png",UriKind.Relative);

did the trick :)

Christian M