Published articles on other web sites*

Published articles on other web sites*

difference between Custom Control and User Control

Generally a UserControl provides the base class for defining a new control that encapsulates related existing controls and provides its own logic. Custom User-Controls are user controls that can be reused but they can't be skinned or themed.
In Windows Phone 7 Custom  User Controls inherit from (System.Windows.Controls.UserControl) UserControl.
Here is how the inheritance schema looks like
Represents an object that participates in the Silverlight dependency property  system. It is the immediate base class of several other important Silverlight classes, such as UIElement, FrameworkTemplate, Style etc.
System.Windows.UIElement is a base class for most of the objects that have visual appearance and can process basic input in Silverlight.
Provides a framework of common APIs for objects that participate in Silverlight layout. System.Windows.FrameworkElement also defines APIs related to data binding, object tree, and object lifetime feature areas in Silverlight.
Represents the base class for UI elements that use a System.Windows.Controls.ControlTemplate  to define their appearance.
  Provides the base class for defining a new control that encapsulates related existing controls and provides its own logic.
You have a XAML file and C# class file for a user control. The class file extends UserControl class, adds the additional behaviour and properties. The XAML file encapsulates the composing controls, the styles, the templates, animations and whatever necessary. Since it is a just composition, it is really easy to create.
NOTE: Use UserControl when you want :
  • to separate functionality into smaller, manageable pieces of logic that can be created independently from an application and other controls.
  • to group related controls that can be used more than once in an application.
NOTE: The major advantage of using UserControl is the ease with which they can be created and reused.
Custom Control
Custom controls can be loosely defined as creating a control that is based on (i.e. derived from) an existing control and extends its functionality in some way.
Custom Controls are skinable, themable and reusable controls that once created can be used by simply loading the assembly in any project.  All controls that are used in Silverlight for Windows Phone 7 (eg., Button, TextBlock, ListBox) and UserControl are also Custom Controls. Generally a Custom Control inherits from:
  • (System.Windows.Controls.Control) Controls
       Represents the base class for UI elements that use a ControlTemplate to define their appearance.
        Represents a control that can be used to present a collection of items.
        Represents a control with a single piece of content. Controls such as ButtonCheckBox, and ScrollViewer directly or indirectly inherit from this class.
  • etc.
NOTE: Use Custom Control when you want to :
  • to add a capability not included in the base controls
  • to implement a completely new control
  • to make a specific version of a control
  • group several controls together to perform a specific function
NOTE: One other advantage of custom controls is that since they are compiled into a .dll, they can be reused in multiple Silverlight for Windows Phone 7 applications where a User Control is limited to the application in which it is created.
NOTE: Custom Controls are harder to understand, but more flexible to use. They enable you to have a total control over the code.
The creation of a custom control certainly requires you to have a better understanding of Silverlight UI model.

Table of Comparison

User Control
Custom Control
Can't be added to toolbox
Full toolbox support
Good for static layout
Good for dynamic layout
Easier to create
Harder to create
Not complied into dll
Compiled in to dll
Represent a set of controls
Implemented on the code level
It is just a normal ContentControl which you can extend a bit and for which you can predefine the content.
Custom control provides greater flexibility
Does not require  too much knowledge of Silverlight UI mode
Require better understanding of Silverlight UI model
UI Is fixed. It Can't be changed in referenced project.
UI is not fixed. It is template. It can be changed in referenced project

In this article I explain in details some of the differences between UserControls and Custom Controls.
As a conclusion I would suggest that you use UserControl when you need a rapid and fixed UI content. Use a Custom Control when you need to implement some specific functionality or want to reuse the control multiple times in different projects. I.e. if you have for example 3 application projects that use the same components it is better to define them as a Custom Control instead of  grouping them into a User Control and writing the same code multiple times.
I hope that the article was helpful.

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...