Change applicationbar buttonicon at runtime

故事扮演 提交于 2019-12-10 14:04:31

问题


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


回答1:


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];



回答2:


figured it out...

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

did the trick :)



来源:https://stackoverflow.com/questions/3718924/change-applicationbar-buttonicon-at-runtime

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!