Objective
Windows Phone provides developers with many native controls, but in many scenarios we need to modify the default features or styles to meet our needs, and custom controls come into being. This article describes how to create and use custom controls in Windows Phone by taking a custom control progress loop (progressring) as an example.
1, Control base class
Custom controls are usually inherited from control, ItemsControl, ContentControl, and so on.
Control: Represents the base class for UI controls that use ControlTemplate to define styles.
System.Object
System.Windows.DependencyObject
System.Windows.UIElement
System.Windows.FrameworkElement
System.Windows.Controls.Control
ItemsControl: Represents a control that can be used to represent a collection object.
System.Object
System.Windows.DependencyObject
System.Windows.UIElement
System.Windows.FrameworkElement
System.Windows.Controls.Control
System.Windows.Controls.ItemsControl
ContentControl: Represents a control that has a single block-level content element. For example, like Button,checkbox,scrollviewer are directly or indirectly inherited from it.
System.Object
System.Windows.DependencyObject
System.Windows.UIElement
System.Windows.FrameworkElement
System.Windows.Controls.Control
System.Windows.Controls.ContentControl
2. Create custom controls
Let's create a class that inherits from the control's progressring user controls.
namespace Windowsphone.controls {public class Progressring:control {public progressring ()
{Defaultstylekey = typeof (Progressring); public override void Onapplytemplate () {base.
Onapplytemplate ();
public bool IsActive {get {return (bool) GetValue (Isactiveproperty);}
set {SetValue (Isactiveproperty, value);} public static readonly DependencyProperty Isactiveproperty = Dependencyproperty.register ("Isact Ive ", typeof (BOOL), typeof (Progressring), new PropertyMetadata (False, New Propertychangedcallback (isactivechanged))
; private static void Isactivechanged (DependencyObject D, DependencyPropertyChangedEventArgs args) {var
PR = (progressring) D; var isactive = (bool) args.
NewValue; }
}
}
By DependencyProperty Isactiveproperty to represent the state of the progress Loop, DependencyProperty differs from the normal attribute, DependencyProperty attribute can be a value expression, data binding, Support for animation and property change notifications. For example, if you declare a style, you can set the background color in the form of <setter property= "Background" value= "Red"/> Because background is a DependencyProperty attribute, you cannot do the same thing with normal properties because they are not DependencyProperty properties.
Defaultstylekey represents the default style, to provide a default style for controls that inherit from control, set the Defaultstylekey property to the TargetType property of the same type. If you do not set Defaultstylekey, the default style of the base class is used. For example, if a control called Newbutton inherits from the Button, to use the new default Style, set the Defaultstylekey to type Newbutton. If you do not set Defaultstylekey, the Style is used for the Button.
In some complex scenarios, such as you want to get an instance of an object in ControlTemplate, it is necessary to override the Onapplytemplate method. This method is called when the control is displayed in front of the screen, where onapplytemplate is more appropriate than the loaded event to adjust the visual tree created by template because the loaded event may be invoked before the page applies the template. Therefore, you may not be able to get an instance of an object in ControlTemplate, and you may not be able to complete the ability to adjust the template before.
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/OS/extra/