用户控件库和自定义控件库有什么区别? [英] What is the difference between a User Control Library and a Custom Control Library?

查看:590
本文介绍了用户控件库和自定义控件库有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想了解WPF,并想创建一个可重用的WPF控件。

I am just coming up to speed on WPF and would like to create a reusable WPF control.

当我查看在Visual Studio中创建项目的选项时,会看到 WPF用户控件库和 WPF自定义控件库。我尚不清楚它们之间有什么区别,我的Google搜索结果还没有出现任何体面的解释。

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.

我想了解它们之间的区别,理想情况下何时使用另一个的一些示例。

I'd like to understand the differences between them and ideally see some examples of when to use one over the other.

推荐答案

在实践中,自定义控件是您在代码级上实现的,可以将XAML用于用户控件。自定义控件扩展了WPF控件基类之一,并通过代码提供了附加功能,因此所有添加的逻辑和表示形式都必须在代码内实现。

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.

从技术上讲,用户控件是一种普通的内容控件,您可以在代码的某些部分进行扩展,但是通常可以通过在其中放置其他控件来对其进行扩展。因此,正如肯特(Kent)所述,UserControl是其他控件的集合。这极大地限制了用户控件的功能。与完整的自定义控件相比,它更易于使用,但更受限制。

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.

从运行时的角度来看,这些控件的差别很小。在构建应用程序并将UserControl放入其中时,控件树将在其中包含一个具体的UserControl模板。因此,如果我们考虑一个专门按钮的la脚例子。如果您使用的是用户控件,则可以在< 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.

尽管自定义控件将提供类似于

While the custom control would provide a logical tree similar to


  • 窗口


    • CustomButton

    UserControl会给出逻辑树

    The UserControl would give a logical tree of


    • 窗口


      • CustomButtonUserControl


        • Button

        最后,UserControl只是一个普通的ContentControl,您可以对其进行扩展,并为此进行扩展可以预定义内容。自定义控件以易于实现为代价提供了更大的灵活性,因为您必须在代码中完成所有逻辑和交互操作,而不是从XAML中受益。

        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.

        所有这些,我认为Visual Studio模板没有太大区别。 Visual Studio自定义控件很可能只创建一个具有空自定义控件的项目,而User Control项目是一个具有空用户控件的项目。您以后可以将任何种类的项目添加到项目中。

        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.

        更新

        我对何时使用自定义控件和用户控件的看法是,如果您可以使用用户控件完成某些操作,而逻辑树中的多余控件元素不会打扰您,请使用用户控件,因为它们非常容易创建和维护。仅当您有理由不使用用户控件时,才使用自定义控件。

        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.

        这篇关于用户控件库和自定义控件库有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆