WPF/Silverlight 中的 UIElement 与 FrameworkElement

发布于 2024-11-06 11:07:16 字数 144 浏览 5 评论 0原文

考虑到 FrameworkElement 继承 UIElement,我何时从 UIElementFrameworkElement 派生。谁能举出现实生活中的例子吗?

When do I derive from UIElement and FrameworkElement considering FrameworkElement inherits UIElement. Can anyone give real life examples?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

唯憾梦倾城 2024-11-13 11:07:16

这是一个了解 WPF 架构的好页面,并且此答案仅适用于 WPF。如果有时间,请查看 UIElementFrameworkElement 部分以及其余部分。以下是链接页面中的一段引文,解释了为什么存在这两个级别:

就本主题而言,WPF 的“核心”功能(在PresentationCore 程序集中实现的功能)一直是焦点。在构建 WPF 时,理想的结果是基础部分(例如使用 Measure 和 Arrange 进行布局的合同)和框架部分(例如特定布局(如 Grid)的实现)之间的明确分离。目标是在堆栈中提供一个低扩展点,允许外部开发人员在需要时创建自己的框架。

简而言之,UIElement 知道如何绘制自己(因为它们源自 Visual)。他们还可以通过提供诸如 之类的虚拟方法来使用路由事件系统 >OnPreviewMouseDownOnMouseDown,以及 布局系统通过实现MeasureArrange

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 and FrameworkElement sections, as well as the rest if you have time. Here's a quote from the linked page explaining why the 2 levels exist:

To this point in the topic, "core" features of WPF – features implemented in the PresentationCore assembly, have been the focus. When building WPF, a clean separation between foundational pieces (like the contract for layout with Measure and Arrange) and framework pieces (like the implementation of a specific layout like Grid) was the desired outcome. The goal was to provide an extensibility point low in the stack that would allow external developers to create their own frameworks if needed.

In short, UIElements know how to draw themselves (because they are derived from Visual). They can also use the routed events system by providing virtual methods like OnPreviewMouseDown and OnMouseDown, and part of the layout system by implementing Measure and Arrange.

FrameworkElements extend the layout system by implementing some of the virtual methods defined in UIElement. They provide a consistent way of setting layout properties, e.g. the Margin property and the MinWidth 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 from UIElement as there is a slight overhead from using FrameworkElement.

Also, you should have a look at the Control class (derived from FrameworkElement), as these provide useful new layers of functionality like Templating and properties like Padding.

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).

我爱人 2024-11-13 11:07:16

我现在没有任何示例,但我可以向您推荐可能有帮助的链接。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文