WPF 中使用的模式
我更多地参与 WPF 已经大约一年了。很多事情都是新的,有时我很难理解它们。
同时我正在重读GOF设计模式这本书。
有几次我会停在中间,因为我意识到某种模式正是某些 WPF 功能中使用的模式。每当我有了这样的认识时,我就感觉我对WPF相关原理的理解就迈出了一大步。这有点像顿悟效应。
我还意识到,例如,我可以更轻松地理解 Prism,因为文档在解释所涉及的模式方面做得非常出色。
所以这是我的“问题”(更像是一种努力):
为了帮助大家理解 WPF 更好,如果有人的话那就太好了 谁还“发现”了一种设计模式 WPF 可以给出简短的解释。
我发现的一个非常明显的例子是路由事件:
如果孩子检测到事件 控制并且没有处理程序 指定的,它将它传递给它的 父母等等,直到最后 已处理或不再找到父级。
假设我们在按钮上有一个图像 位于 StackPanel 内 在一个窗口内。如果用户点击 图像,事件将是 由它处理(如果处理代码有 已指定)或“冒泡”直到 其中一个控件负责处理它。所以 每个控件都有机会 按此顺序进行反应。
- 图片
- 按钮
- 堆栈面板
- 窗口
一旦控件处理它, 冒泡将会停止。
这是简短的解释,对于 更准确的请参考WPF 文学。
这种功能代表 “责任链 设计模式”指出,如果 他们是一个请求,它被通过了 沿着责任链给予 其中的每个对象都有机会处理 它。请求的发送者没有 知道谁来处理它,这可以确保 脱钩。为了更彻底 解释请点击链接。
这里的目的只是为了展示这个(看起来已经有 10 多年历史的)想法如何融入我们当前的技术,并提供另一种看待它的方式。
我认为这对于一个开始来说已经足够了,并希望这里能收集到更多的相似之处。
干杯,托尔斯滕
I have been getting more involved with WPF for about a year now. A lot of things are new and sometimes it is hard to get my head wrapped around it.
At the same time I am rereading the GOF Design Patterns book.
A few times I would stop in the middle because I would realize that a certain pattern is the very one used in some WPF functionality. Whenever such a realization hits me, I feel like my understanding of the related WPF principle just took a big leap. It's kind of like an aha-effect.
I also realized that I had a much easier time understanding Prism for example because the documentation does such a great job at explaining the patterns involved.
So here is my "question" (more like an effort):
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.
One pretty obvious example that I found is the Routed Event:
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.
The purpose here is merely to show how this (seemingly old 10+ years) idea found its way into our current technology and to offer another way of looking at it.
I think this is enough for a start and hope more parallels will be collected here.
Cheers, Thorsten
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不认为它是 WPF 特有的,但观察者设计模式似乎是 .Net 和 WPF 中所有事件处理的基础。
观察者设计模式被描述为“定义对象之间的一对多依赖关系,以便当一个对象改变状态时,它的所有依赖者都会得到通知并自动更新”。在 .Net 中,您可以使用 += 运算符来订阅这样的状态更改。随后您可以使用 -= 运算符取消订阅。
I don't think it is specific for WPF but the observer design pattern seems to be the foundation on which all event handling in .Net and WPF is based.
The observer design pattern is described as "Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.". In .Net with the += operator you subscribe to such a change in state. Subsequently you unsubscribe with the -= operator.
我想说 CommandBindings 对于我的开发方式非常重要和基础。
I'd say CommandBindings are pretty important and fundamental to the way I develop.