开发者

Have object send message to 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 point开发者_如何学Cs textblock? thanks for any advice


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:

  • WPF Apps With The Model-View-ViewModel Design Pattern (still applied to Silverlight as a methodoloogy)
  • Model-View-ViewModel In Silverlight 2 Apps

If you need samples, take a look here.


Are Class Target and AnotherClass.points within the same name space? If so, may be the following helps:

/* In Window class */
Window w = new Window();

/* function where Target get hit */
w = this;
/* code to update points */
w.textbox1.Text = AnotherClass.points.ToString();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜