It's nice to not have to refer to UI stuff in the backend. I assume this is the reason you're wanting to do this.
If that's the case, what you could do is implement some Publish/subscribe pattern (such as the Observer pattern). That way, you don't have to refer specifically to the UI. You can just "publish" from your backend, and subscribe to those events from your UI.
Alternatively, you could inherit your UI from an interface, and specify the methods you need on that. Then, refer only to the interface in your backend.
Unfortunately you don't really state what you are using to build your UI. But the whole problem can quickly disappear with WPF (or Silverlight) and data binding. In a nutshell items in the UI are bound to properties and commands in a backing class. When a property changes the PropertyChanged event is raised and the UI knows to update itself. For more information start searching on MVVM.
At the end of the day, this is actually a form of the observer pattern, but you don't have to do all the wiring yourself.
发布评论
评论(2)
不必在后端引用 UI 内容,这很好。我认为这就是您想要这样做的原因。
如果是这种情况,您可以做的是实现一些发布/订阅模式(例如观察者模式)。这样,您就不必专门参考 UI。您可以从后端“发布”,然后从 UI 订阅这些事件。
或者,您可以从接口继承 UI,并指定您需要的方法。然后,仅参考后端的接口。
It's nice to not have to refer to UI stuff in the backend. I assume this is the reason you're wanting to do this.
If that's the case, what you could do is implement some Publish/subscribe pattern (such as the Observer pattern). That way, you don't have to refer specifically to the UI. You can just "publish" from your backend, and subscribe to those events from your UI.
Alternatively, you could inherit your UI from an interface, and specify the methods you need on that. Then, refer only to the interface in your backend.
不幸的是,您并没有真正说明您使用什么来构建用户界面。但通过 WPF(或 Silverlight)和数据绑定,整个问题可以很快消失。简而言之,UI 中的项目绑定到支持类中的属性和命令。当属性更改时,会引发 PropertyChanged 事件,并且 UI 会自行更新。要了解更多信息,请开始在 MVVM 上搜索。
归根结底,这实际上是观察者模式的一种形式,但您不必自己完成所有接线。
Unfortunately you don't really state what you are using to build your UI. But the whole problem can quickly disappear with WPF (or Silverlight) and data binding. In a nutshell items in the UI are bound to properties and commands in a backing class. When a property changes the PropertyChanged event is raised and the UI knows to update itself. For more information start searching on MVVM.
At the end of the day, this is actually a form of the observer pattern, but you don't have to do all the wiring yourself.