在运行时更改应用程序栏按钮图标 [英] Change applicationbar buttonicon at runtime

查看:41
本文介绍了在运行时更改应用程序栏按钮图标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个 WP7 应用程序,该应用程序需要根据请求的状态更改应用程序栏上按钮的图标.我试过了:

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 :)

谢谢

推荐答案

ApplicationBar 是一个特殊的控件,它的行为不像其他 Silverlight 控件(请参阅 Peter Torr 关于该主题的帖子).后果之一是在 XAML 中为应用栏按钮指定的名称会在代码隐藏中生成始终为空的字段.

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.

我猜在您的情况下,DetailsAppBar 中的 btnStart 字段设置为 null,因此尝试设置其 IconUri 属性会导致抛出 NullReferenceException.

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

这篇关于在运行时更改应用程序栏按钮图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆