wpf - WPF 中使用的模式

标签 wpf chain-of-responsibility design-patterns

我已经更多地参与 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.

  1. Image
  2. Button
  3. StackPanel
  4. 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/

相关文章:

c# - 如何在wpf中调用按钮的样式函数?

c# - 如何在属性更改时触发 DataTemplateSelector?

java - 如何降低if中多个条件的圈复杂度?

java - 如何以可测试的方式实现责任链?

oop - 命令模式如何参数化具有不同请求的客户端?

javascript - 了解基本js设计模式中的命令模式

c# - 在哪里可以找到列表中可单击项目使用的 Windows Phone 按钮样式?

c# - 创建文本框问题?

Swift 编译器无法解决泛型的递归使用

c++ - C++中实例变量的算法初始化