吴佳鑫的个人专栏

当日事当日毕,没有任何借口

导航

silverlight 之Control and UserControl 区别

先来一张UML图

 

最简单的来说,UserControl 是Control 的一个子类。

但是在实际应用中,它们会给平等的对待。有时候我们的Control会让它直接继承 Control这个类,有的时候会直接继承于UserControl 这个类。

 

区别:

1、UserControl 它实际上是已有控件的组合(textbox , button ....),它的功能是做组合;

     而Control 主要是做功能,就是做一些有独特功能的控件。

2、UserControl 主要是对一些已有control之间的联动,  像我们要有一个翻页功能,点击了左键要翻页,点击了右键要翻页什么的,这种比较多的情况是一个UserControl , 可以实现比较多的control之间的复杂的关系。像这样的情况一般就使用UserControl,例如我们定义一个myUserControl 去继承于 UserControl来做这样的事。

而继承于Control 的话,就主要是这个控件本身的功能和外观上。

 

3、UserControl是在设计时形成外观的,而Control是在运行时形成外观的。

4、继承于Control的话,它的 Template和Style 是可以被赋值重新定义的变换他的外观和交互方式的,它只有一个CS文件,只是在运行时给它的Template 或者style设值,它才有外观,否则的话它没有外观。

而UserControl是不可以的,它是由一个XAML文件和一个CS文件一起去定义的。

5、当继承于Control 和 UserControl 的A 和 B , 我们是可以再用一个Control 来继承于 A的,但不能再用一个UserControl来继承B的。

所以一个继承control的东西可以做得很活。

 

 

应用场景:

如果是要做组合型逻辑的话,比较高层次的一个Page的话,就用UserControl.

如果是要做一个比较独立的控件,想要这个控件在不同的地方有不同的使用方式,不同的外观,那我们用Control.

 

 //另:网上找到的资料

•Relationship
–UserControl Inherit Control     -- usercontrol 继承于 control
•Difference
–Control: Visual Is Set in Running Time    -- 运行时可见
–UserControl: Visual Is Set in Development Time  --设计时可见
–Control: MyControl Can be Inherited        --能被继承
–UserControl: MyUserControl Can’t be Inherited  --不能被继承
–Control: C# Can’t Call Visual Element using ‘this.’    --不能通过this. 来找到XAML中的元素
–UserControl: C# Can Call Visual Element using ‘this.’  --可以通过this.来找到XAML中的元素
•Usage
–Control: Focus on Data and Feature      --关注于数据与特征
–UserControl: Focus on Visual                --关注于逻辑组合显示
–Control: Template may Be Changed Frequently   --模板可以被重新设置
–UserControl: Visual Is Merged by Some Controls Stably
–Control: Using Data Binding         --使用绑定
–UserControl: Using Code Management   --后台代码

posted on 2012-07-29 00:09  _eagle  阅读(1190)  评论(0编辑  收藏  举报