C# 何时添加组件类与用户控件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1455998/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
When to add a Component Class vs User Control?
提问by alexD
I have a general idea, and there are some obvious cases, but there are also some gray areas for me - when is it best to use to extend from a component and when is it best to create a user control? This pertains to a specific work problem I am trying to solve, but the specifics of that are not important - a general answer to this question is enough for me.
我有一个大致的想法,并且有一些明显的情况,但对我来说也有一些灰色区域 - 什么时候最好使用从组件扩展,什么时候最好创建用户控件?这与我试图解决的特定工作问题有关,但具体问题并不重要 - 对这个问题的一般答案对我来说就足够了。
采纳答案by Reed Copsey
In WPF and Windows Forms, both, the main difference is that a UserControl is meant to be a collection of controls - a reusable, single object "composed" from multiple controls themselves.
在 WPF 和 Windows 窗体中,主要区别在于 UserControl 旨在成为控件的集合 - 一个可重用的、由多个控件本身“组成”的单个对象。
You'd impelemnt a Component/CustomControl/Control instead of a UserControl if you are making a single, primitive control with new behavior, instead of making a "control" that's composed of smaller controls. Component usually is a non-visual behavior, where a CustomControl/Control is usually for a visual control.
如果您正在制作具有新行为的单个原始控件,而不是制作由较小控件组成的“控件”,则您将实现 Component/CustomControl/Control 而不是 UserControl。Component 通常是一个非可视化的行为,其中一个 CustomControl/Control 通常是一个可视化的控件。
回答by Fredrik M?rk
In general, I would use Component
when the control does not have any user interface (or at least not one that is present on the form). If it is a UI control I would create a User Control
instead.
一般来说,我会Component
在控件没有任何用户界面(或至少没有出现在表单上的用户界面)时使用。如果它是一个 UI 控件,我会创建一个 User Control
。
回答by Travis Heseman
I typically extend Control, or more ofter UserControl, only when I want to package some UI functionality. For Components, I think of the classic example, the Timer. It can be dropped onto the designer, configured through the Properties pane, and then accessed programmatically through the code behind. In short, I extend Component when I want to be able to manipulate some bundled-up state and behavior, an object with no UI, through the designer.
我通常只在我想打包一些 UI 功能时才扩展 Control,或者更多的是 UserControl。对于组件,我想到了一个经典的例子,Timer。它可以放到设计器上,通过“属性”窗格进行配置,然后通过背后的代码以编程方式访问。简而言之,当我希望能够通过设计器操作一些捆绑的状态和行为(一个没有 UI 的对象)时,我会扩展 Component。
回答by jrista
There is one significant difference between a Component and a Control: Controls have user interface. All controls are also components, but not all components are controls. If you need to display a user interface, deriving from some kind of Control base (Control, UserControl, Form, etc.) would usually be required. If you just have behavior, such as with the BackgroundWorker component, then you would only need to derive from Component directly.
组件和控件之间有一个显着区别:控件具有用户界面。所有控件也是组件,但并非所有组件都是控件。如果您需要显示用户界面,通常需要从某种 Control 基础(Control、UserControl、Form 等)派生。如果您只有行为,例如使用 BackgroundWorker 组件,那么您只需要直接从 Component 派生。
Another note...both Components and Controls may be dropped onto a design surface. Components show up as an icon and a label in a special area, controls appear directly on the design surface. However, there is a third thing that you may use: a simple Class. If you do not need design surface support, I would recommend using a simple class rather than Component or Control. They are lighter in weight and less bloated when all you need is 100% pure behavior with no design-time support.
另一个注意事项...组件和控件都可以放在设计图面上。组件在特殊区域中显示为图标和标签,控件直接出现在设计图面上。但是,您可以使用第三种东西:一个简单的类。如果您不需要设计图面支持,我建议您使用简单的类而不是 Component 或 Control。当您只需要 100% 纯行为而无需设计时支持时,它们的重量更轻且体积更小。