我已经更多地参与 WPF 大约一年了。很多东西都是新的,有时我很难理解它。
同时我正在重读 GOF 设计模式这本书。
有几次我会在中间停下来,因为我会意识到某种模式正是在某些 WPF 功能中使用的模式。每当这样的认识击中我时,我都觉得我对相关 WPF 原理的理解有了很大的飞跃。这有点像啊哈效应。
我还意识到,例如,我可以更轻松地理解 Prism,因为文档在解释所涉及的模式方面做得非常出色。
所以这是我的“问题”(更像是一种努力):
In order to help us all to understand WPF better it would be great if anyone who also "spotted" a design pattern in WPF could give a short explanation.
我发现的一个非常明显的例子是路由事件:
If an event is detected by a child control and no handler has been specified, it passes it along to its parent and so on until it is finally handled or no parent is found anymore.
Lets say we have an image on a button that is inside a StackPanel that is inside a window. If the user clicks the image, the event will either be handled by it (if handling code has been specified) or "bubble" up until one of the controls handles it. So each control will get a chance to react in this order.
- Image
- Button
- StackPanel
- Window
Once a control handles it, the bubbling will stop.
This is the short explanation, for a more precise one consult the WPF literature.
This kind of functionality represents the "Chain of Responsibility Design Pattern" which states, that if their is a request, it gets passed along a responsibility chain to give each object in it a chance to handle it. The sender of the request has no idea who will handle it which ensures decoupling. For a more thorough explanation follow the link.
这里的目的只是为了展示这个(看似古老的 10 多年)想法如何进入我们当前的技术,并提供另一种看待它的方式。
我认为这足以作为一个开始,并希望在这里收集更多的相似之处。
干杯,托尔斯滕
最佳答案
我不认为它特定于 WPF,但观察者设计模式似乎是 .Net 和 WPF 中所有事件处理所基于的基础。
观察者设计模式被描述为“定义对象之间的一对多依赖关系,这样当一个对象改变状态时,它的所有依赖项都会被自动通知和更新。”。在带有 += 运算符的 .Net 中,您订阅了这种状态变化。随后您使用 -= 运算符取消订阅。
关于wpf - WPF 中使用的模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1770391/