wpf 用户控件库和自定义控件库有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/807703/
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 a User Control Library and a Custom Control Library?
提问by 17 of 26
I am just coming up to speed on WPF and would like to create a reusable WPF control.
我只是想加快 WPF 的速度,并想创建一个可重用的 WPF 控件。
When I look at the options for creating projects in Visual Studio, I see "WPF User Control Library" and "WPF Custom Control Library". It's unclear to me what the difference is between them and my Google searches have not turned up any decent explanations.
当我查看在 Visual Studio 中创建项目的选项时,我看到了“WPF 用户控件库”和“WPF 自定义控件库”。我不清楚它们之间有什么区别,我的谷歌搜索没有找到任何合适的解释。
I'd like to understand the differences between them and ideally see some examples of when to use one over the other.
我想了解它们之间的差异,并在理想情况下看到一些何时使用一个而不是另一个的例子。
采纳答案by Mikko Rantanen
In practice custom controls are something you implement on the code level while you can use XAML for user controls. The custom controls extend one of the WPF control base classes and provide additional functionality through code so all the added logic and representation must be implemented inside the code.
在实践中,自定义控件是您在代码级别实现的,而您可以将 XAML 用于用户控件。自定义控件扩展了 WPF 控件基类之一,并通过代码提供附加功能,因此所有添加的逻辑和表示都必须在代码中实现。
A user control is technically a normal content control which you can extend in some parts in the code but usually it is extended by placing other controls inside it. So as Kent mentioned a UserControl is an aggregation of other controls. This limits what you can do with a user control considerably. It's easier to use but more limited than a full custom control.
用户控件在技术上是一个普通的内容控件,您可以在代码的某些部分对其进行扩展,但通常通过在其中放置其他控件来扩展它。正如 Kent 提到的,UserControl 是其他控件的聚合。这极大地限制了您可以使用用户控件执行的操作。它更易于使用,但比完全自定义控件更受限制。
These controls have a small difference from a runtime point of view. When building an application and placing an UserControl into it, the control tree will have a concrete UserControl template inside of it. So if we consider a lame example of a specialized button. If you were using a user control you'd add a button inside the <UserControl>
element. When using a custom control you'd derive the control itself from a button most likely. The difference would be visible in the logical tree.
从运行时的角度来看,这些控件有很小的区别。在构建应用程序并将 UserControl 放入其中时,控件树将在其中包含一个具体的 UserControl 模板。因此,如果我们考虑一个专门按钮的蹩脚示例。如果您使用的是用户控件,则需要在<UserControl>
元素内添加一个按钮。使用自定义控件时,您最有可能从按钮派生控件本身。差异将在逻辑树中可见。
While the custom control would provide a logical tree similar to
虽然自定义控件将提供类似于
- Window
- CustomButton
- 窗户
- 自定义按钮
The UserControl would give a logical tree of
UserControl 将给出一个逻辑树
- Window
- CustomButtonUserControl
- Button
- CustomButtonUserControl
- 窗户
- 自定义按钮用户控件
- 按钮
- 自定义按钮用户控件
So in the end the UserControl is just a normal ContentControl which you can extend a bit and for which you can predefine the content. Custom control provides greater flexibility at the price of ease of implementation as you have to do all the logic and interaction in the code instead of having the benefit of XAML.
因此,最终 UserControl 只是一个普通的 ContentControl,您可以对其进行一些扩展,并且您可以为其预定义内容。自定义控件以易于实现为代价提供了更大的灵活性,因为您必须在代码中执行所有逻辑和交互,而不是利用 XAML 的好处。
Though after all this, I don't think there's that much difference in the Visual Studio templates. Most likely the Visual Studio Custom Control just creates a project with an empty custom control while the User Control project is a project with an empty user control. You can later add any kind of items to the project.
尽管如此,我认为 Visual Studio 模板并没有太大区别。很可能 Visual Studio 自定义控件只是创建一个带有空自定义控件的项目,而用户控件项目是一个带有空用户控件的项目。您可以稍后向项目添加任何类型的项目。
Update
更新
And my opinion on when to use custom control and user control is that if you can get something done with a user control and the extra control element in the logical tree doesn't bother you, use a user control as they are so much easier to create and maintain. Use a custom control only if you have a reason not to use a user control.
我对何时使用自定义控件和用户控件的看法是,如果您可以使用用户控件完成某些操作并且逻辑树中的额外控件元素不会打扰您,请使用用户控件,因为它们更容易创建和维护。仅当您有理由不使用用户控件时才使用自定义控件。