WPF/Silverlight 中的 UIElement 与 FrameworkElement
考虑到 FrameworkElement
继承 UIElement
,我何时从 UIElement
和 FrameworkElement
派生。谁能举出现实生活中的例子吗?
When do I derive from UIElement
and FrameworkElement
considering FrameworkElement
inherits UIElement
. Can anyone give real life examples?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个了解 WPF 架构的好页面,并且此答案仅适用于 WPF。如果有时间,请查看
UIElement
和FrameworkElement
部分以及其余部分。以下是链接页面中的一段引文,解释了为什么存在这两个级别:简而言之,
UIElement
知道如何绘制自己(因为它们源自 Visual)。他们还可以通过提供诸如之类的虚拟方法来使用路由事件系统 >OnPreviewMouseDown
和OnMouseDown
,以及 布局系统通过实现Measure
和Arrange
。FrameworkElement
通过实现UIElement
中定义的一些虚拟方法来扩展布局系统。它们提供了设置布局属性的一致方法,例如Margin
属性和MinWidth
属性。此外,可以设置样式,并且它们可以参与数据绑定 。在回答您的问题时,如果您需要
FrameworkElement
添加的任何额外功能,例如您需要样式、绑定或更易于使用的布局系统,请从它们派生。否则,请从UIElement
派生,因为使用FrameworkElement
会产生轻微的开销。另外,您应该查看
Control
类(派生自FrameworkElement
),因为它们提供了有用的新功能层,例如 模板 和Padding
等属性。熟悉继承层次结构也是一个好主意,您可能想要从其中的其他类派生(尽管可能不高于
Visual
链)。This is a good page for learning about WPF Architecture, and this answer only applies to WPF. Check out the
UIElement
andFrameworkElement
sections, as well as the rest if you have time. Here's a quote from the linked page explaining why the 2 levels exist:In short,
UIElement
s know how to draw themselves (because they are derived from Visual). They can also use the routed events system by providing virtual methods likeOnPreviewMouseDown
andOnMouseDown
, and part of the layout system by implementingMeasure
andArrange
.FrameworkElement
s extend the layout system by implementing some of the virtual methods defined inUIElement
. They provide a consistent way of setting layout properties, e.g. theMargin
property and theMinWidth
property. Additionally, the can be styled, and they can take part in data binding.In answer to your question, if you need any of the extra abilities that
FrameworkElement
add, e.g. you need styles, binding or a layout system that's easier to use, then derive from them. Otherwise, derive fromUIElement
as there is a slight overhead from usingFrameworkElement
.Also, you should have a look at the
Control
class (derived fromFrameworkElement
), as these provide useful new layers of functionality like Templating and properties likePadding
.Familiarising yourself with the inheritance hierarchy is also a good idea, you might want to derive from other classes in it (though probably no higher up the chain than
Visual
).我现在没有任何示例,但我可以向您推荐可能有帮助的链接。
UIElement 是一个大多数具有视觉外观并可以在 Silverlight 中处理基本输入的对象的基类。
FrameworkElement 提供参与 Silverlight 布局的对象的通用 API 框架。 FrameworkElement 还定义了与 Silverlight 中的数据绑定、对象树和对象生命周期功能区域相关的 API。
那么您可以获得哪些额外功能呢?请参阅http://forums.silverlight.net/p/205863/482651.aspx
I don't have any examples right now, but I can refer you to links that might help.
UIElement is a base class for most of the objects that have visual appearance and can process basic input in Silverlight.
FrameworkElement provides a framework of common APIs for objects that participate in Silverlight layout. FrameworkElement also defines APIs related to data binding, object tree, and object lifetime feature areas in Silverlight.
So what additional capabilities do you get? See http://forums.silverlight.net/p/205863/482651.aspx