交互性.行为与附加属性
我试图找出这些方法之间的一些差异。是否存在使用行为但无法使用附加属性完成相同功能的情况?
I'm trying to find some differences between these approaches. Is there any situation where behaviors are used and the same functionality could not be done with attached properties?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不。行为基本上只是附加属性之上的更好的抽象。
通过使用
Behavior
,您可以直接访问AssociatedObject
,并能够在运行时轻松附加和分离行为。您可以使用附加属性来做到这一点,但这需要添加大量额外的管道。
No. Behaviors are basically just a much nicer abstraction on top of attached properties.
By using
Behavior<T>
, you gain access to theAssociatedObject
directly, as well as the ability to attach and detach the behavior, easily, at runtime.You could do this with attached properties, but it would require adding a lot of extra plumbing.
我倾向于使用行为来添加可进行可见更改的功能。而我使用附加属性向对象添加附加信息,随后由其他对象使用。
例如,Grid.Row 是一个很好的附加属性,因为它由网格而不是目标使用。另一方面,自动更正会产生良好的行为,因为这将对对象进行可见的更改。
I tend to use Behaviors to add functionality which makes visible changes. Whereas I use attached properties to add additional information to an object which is subsequently used by other objects.
E.g. Grid.Row makes a good attached property, as it's used by the Grid and not the target. On the other hand, AutoCorrect would make a good behaviour, as this will make visible changes on the object.
行为基于附加属性。这意味着如果您找不到特定的行为 - 您可以根据 Blend 提供的行为框架或创建自己的 AP 编写自己的行为...
我一直认为行为是附加属性功能的重要证据。与他们一起你能得到令人难以置信的东西。
Behaviors are based on attached properties. That means if you can't find specific behavior - you write your own, either based on behaviors framework provided by Blend or by creating your own AP...
I always thought that behaviors are great evidence of attached properties power. Just incredible what you can get with them.
这不是答案。但我能找到关于这个主题的最好解释
https://web.archive.org/web/20180208143035/http://briannoyesblog.azurewebsites.net/2012/12/20/attached-behaviors-vs-attached-properties-vs -blend-behaviors/
Brian Noyes 在他的文章中清楚地描述了每个概念之间的差异。
This is not the answer. But the best explanation I can find on this topic
https://web.archive.org/web/20180208143035/http://briannoyesblog.azurewebsites.net/2012/12/20/attached-behaviors-vs-attached-properties-vs-blend-behaviors/
Brian Noyes in his articles clearly describes the differences between each of the concepts.