用户控制库和自定义控制库的区别是什么?

时间:2021-10-23 03:50:49

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.

我想了解它们之间的不同之处,并在理想的情况下看看什么时候使用它们。

2 个解决方案

#1


101  

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模板。因此,如果我们考虑一个专门按钮的蹩脚例子。如果您使用的是用户控件,则在 元素中添加一个按钮。当使用自定义控件时,您最可能是从按钮派生控件本身。差异在逻辑树中是可见的。

While the custom control would provide a logical tree similar to

而自定义控件将提供类似的逻辑树

  • Window
    • CustomButton
    • CustomButton
  • 窗口CustomButton

The UserControl would give a logical tree of

UserControl会给出一个逻辑树

  • Window
    • CustomButtonUserControl
      • Button
      • 按钮
    • CustomButtonUserControl按钮
  • 窗口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.

所以最终用户控件只是一个普通的内容控件,你可以对其进行扩展,并对其进行预定义。自定义控件以易于实现为代价提供了更大的灵活性,因为您必须在代码中执行所有逻辑和交互,而不是使用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.

我对何时使用自定义控件和用户控件的看法是,如果您可以使用用户控件完成一些事情,并且逻辑树中的额外控件元素不会打扰您,那么使用用户控件,因为它们更容易创建和维护。只有在有理由不使用用户控件的情况下,才使用自定义控件。

#2


21  

A Control represents some behavior that is skinnable (templatable), whereas a UserControl is generally a higher-level aggregation of Controls that is specific to an application.

控件表示可skinnable (templatable)的一些行为,而UserControl通常是特定于应用程序的高级控件聚合。

More info available here.

更多信息。

#1


101  

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模板。因此,如果我们考虑一个专门按钮的蹩脚例子。如果您使用的是用户控件,则在 元素中添加一个按钮。当使用自定义控件时,您最可能是从按钮派生控件本身。差异在逻辑树中是可见的。

While the custom control would provide a logical tree similar to

而自定义控件将提供类似的逻辑树

  • Window
    • CustomButton
    • CustomButton
  • 窗口CustomButton

The UserControl would give a logical tree of

UserControl会给出一个逻辑树

  • Window
    • CustomButtonUserControl
      • Button
      • 按钮
    • CustomButtonUserControl按钮
  • 窗口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.

所以最终用户控件只是一个普通的内容控件,你可以对其进行扩展,并对其进行预定义。自定义控件以易于实现为代价提供了更大的灵活性,因为您必须在代码中执行所有逻辑和交互,而不是使用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.

我对何时使用自定义控件和用户控件的看法是,如果您可以使用用户控件完成一些事情,并且逻辑树中的额外控件元素不会打扰您,那么使用用户控件,因为它们更容易创建和维护。只有在有理由不使用用户控件的情况下,才使用自定义控件。

#2


21  

A Control represents some behavior that is skinnable (templatable), whereas a UserControl is generally a higher-level aggregation of Controls that is specific to an application.

控件表示可skinnable (templatable)的一些行为,而UserControl通常是特定于应用程序的高级控件聚合。

More info available here.

更多信息。