Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change applicationbar buttonicon at runtime

I'm developing a WP7 app and the application 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

like image 658
Christian M Avatar asked Sep 15 '10 15:09

Christian M


2 Answers

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];
like image 58
Andréas Saudemont Avatar answered Nov 04 '22 05:11

Andréas Saudemont


figured it out...

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

did the trick :)

like image 45
Christian M Avatar answered Nov 04 '22 07:11

Christian M