C# 类解耦

发布于 2024-10-01 11:05:57 字数 1436 浏览 3 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

失与倦" 2024-10-08 11:05:58

不必在后端引用 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.

誰認得朕 2024-10-08 11:05:58

不幸的是,您并没有真正说明您使用什么来构建用户界面。但通过 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文