C# 用户控件、自定义控件和组件之间有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1322451/
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
What is the difference between User Control, Custom Control and Component?
提问by Svish
These are three different things you can add to a project and I am not quite sure if I understand the difference. They all seem to for example show up in the component toolbox when working with a Form
. What are some common usage scenarios for each of them? What is the difference?
这是您可以添加到项目中的三种不同的东西,我不太确定我是否理解其中的区别。例如,当使用Form
. 它们各自有哪些常见的使用场景?有什么不同?
采纳答案by Fredrik M?rk
The main difference between User Control, Custom Control and Component is that they inherit from different levels in the inheritance tree:
用户控件、自定义控件和组件之间的主要区别在于它们从继承树中的不同级别继承:
MyComponent
|-> Component
MyCustomControl
|-> Control
|-> Component
MyUserControl
|-> ContainerControl
|-> ScrollableControl
|-> Control
|-> Component
So, in short you get a different amount of pre-wired functionality with the different options.
因此,简而言之,您可以通过不同的选项获得不同数量的预接线功能。
When would you use the different options? (these are thoughts and opinions, not truths)
你什么时候会使用不同的选项?(这些是想法和意见,不是事实)
- Create a componentif you want to provide functionality without UI(such as Timer components, data sources, ...)
- Create a custom controlif you want to make a component where you have full control over its visual appearance, and you don't want any baggage of unnecessary functionality. Typical cases would be simple controls with limited functionality (such as a button)
- Create a user controlif you are going to combine existing controlsinto reusable building blocks (such as two lists with buttons where you can move items between the lists).
- 如果您想提供没有 UI 的功能(例如计时器组件、数据源等),请创建一个组件
- 如果您想要制作一个可以完全控制其视觉外观的组件,并且您不想要任何不必要的功能包袱,请创建一个自定义控件。典型情况是功能有限的简单控件(例如按钮)
- 如果您要将现有控件组合成可重用的构建块(例如两个带有按钮的列表,您可以在列表之间移动项目),请创建一个用户控件。
回答by Chad Yeates
Adding to what Fredrik said, generally components and custom controls would be used when you plan on reusing them across projects. Use user controls when you would only be using it in one project.
除了 Fredrik 所说的之外,当您计划跨项目重用组件和自定义控件时,通常会使用它们。当您仅在一个项目中使用用户控件时,请使用它。
回答by Menard Laval
This is some difference between a CustomControl and a UserControl :
这是 CustomControl 和 UserControl 之间的一些区别:
Custom Control
自定义控件
A loosely coupled control w.r.t code and UI
一个松散耦合的控件 wrt 代码和 UI
Derives from Control
源自控制
Defines UI in a ResourceDictionary
在 ResourceDictionary 中定义 UI
UI is skinable
UI是可换肤的
Has dynamic layout
有动态布局
UI can be changed in different projects
UI可以在不同的项目中改变
Has full toolbox support
有完整的工具箱支持
Defines a single control
定义单个控件
More flexible
更灵活
==============
==============
User Control
用户控制
A tightly coupled control w.r.t code and UI
一个紧密耦合的控件 wrt 代码和 UI
Derives from UserControl
派生自 UserControl
Defines UI as normal XAML
将 UI 定义为普通 XAML
Child controls are skinable
子控件是可换肤的
Has static layout
有静态布局
UI is fixed and can't have different looks in different project
UI 是固定的,在不同的项目中不能有不同的外观
Can't be added to the toolbox
无法添加到工具箱
Defines a set of controls
定义一组控件
Not very flexible like a Custom Control
不像自定义控件那样灵活
回答by Himanshu Namdeo
The main difference between them-User Control is a page file with extension .ascx which can only be used within a single application or project But custom controls are assemblies(dll files) that can be used in multiple applications.
它们之间的主要区别 -用户控件是扩展名为 .ascx 的页面文件,它只能在单个应用程序或项目中使用,但自定义控件是可以在多个应用程序中使用的程序集(dll 文件)。
回答by Robopro
I believe the last statement is not correct in my opinion . I create user controls for many different reasons.
我相信最后一句话在我看来是不正确的。我出于许多不同的原因创建用户控件。
The main reason is so that if per say I design an interface of multiple controls grouped together.
主要原因是,如果说我设计了一个由多个控件组合在一起的界面。
I first create a class library , then I add user controls to it . Now if i need to change any part of the logic behind how the user control works I can very easily. Also this class library can be used multiple times.
我首先创建一个类库,然后向其中添加用户控件。现在,如果我需要更改用户控件如何工作背后的逻辑的任何部分,我可以很容易地。这个类库也可以多次使用。
Also within the same classy library I can have multiple classes that can be shared and used for any of my user controls.
同样在同一个经典库中,我可以拥有多个类,这些类可以共享并用于我的任何用户控件。
This is the main reason I use user controls. And if you make a change to your user control or class library . once you build the job . the dll will dynamically up date in the bin folder .
这是我使用用户控件的主要原因。如果您对用户控件或类库进行更改。一旦你建立了工作。dll 将在 bin 文件夹中动态更新。
So if i am referencing this in another project Those changes will also appear in new project .
因此,如果我在另一个项目中引用它,这些更改也将出现在新项目中。
Also it doesn't use the same paint routines as the form and anything you have loaded on the form.
此外,它不使用与表单和您在表单上加载的任何内容相同的绘制例程。
So user controls gives us the ability to be very modular And i Can have multiple user controls that share the basics classes of the class library ... So a user control purpose is just not for one project . It has no limitations in that respect. jeff
所以用户控件给了我们非常模块化的能力而且我可以有多个用户控件共享类库的基础类......所以用户控件的目的不是为了一个项目。它在这方面没有任何限制。杰夫