让对象向 MainPage 发送消息
我有一个类 Target
和一个静态属性,在另一个类中,称为 points
。每次用户击中目标时,我都想增加分数。我将 points
放在不同的静态类中的原因是每个 Target
对象都可以访问它。 问题是显示点的文本块存在于 MainPage 中,而不是每个 Target 对象中。由于我无法将 XAML 绑定到静态属性,因此如何才能使每个 Target 对象能够以某种方式让 MainPage 知道它应该更新点文本块?感谢您的任何建议
I have a class Target
and a static property, in another class, called points
. Each time the user hits a target, I want to increase the points. The reason I put points
in a different static class is so that each Target
object can access it.
The problem is that the textblock displaying the points exists in the MainPage and not in each Target object. Since I can't bind my XAML to a static property, how can I make it so that each Target object can somehow let the MainPage know that it should update the points textblock? thanks for any advice
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你完全可以在这里应用 MVVM 模式。如果有一个绑定到主窗口的静态 ViewModel,那么您可以在每次属性更改时发出通知,并且 Views(显示数据的所有绑定窗口) )将自动更新(重新绑定)。
我建议检查 Laurent Bugnion 的 MVVM Light 框架。它为您完成了很多管道工作,因此您需要做的就是将各个部件放在一起并正确绑定它们。
一些值得根据您的情况检查的资源:
模型-视图-视图模型设计
模式(仍然适用于
Silverlight 作为一种方法)
2 个应用程序
如果您需要示例,请查看此处。
You could totally apply the MVVM pattern here. If there is a static ViewModel that is bound to the main window, then you can raise a notification each time a property changes and the Views (all tied windows that display the data) will be automatially updated (re-bound).
I would recommend checking Laurent Bugnion's MVVM Light framework. It has a lot of this plumbing done for you, so all that needs to be done from your side is to put the parts together and bind them correctly.
Some resources worth checking for your situation:
Model-View-ViewModel Design
Pattern (still applied to
Silverlight as a methodoloogy)
2 Apps
If you need samples, take a look here.
Class Target 和 AnotherClass.points 是否在同一名称空间内?如果是这样,以下可能会有所帮助:
Are Class Target and AnotherClass.points within the same name space? If so, may be the following helps: