Create custom controls on Windows Phone 7

Source: Internet
Author: User

Create a custom control:
The control class must be derived from the control (or contentcontrol). At least, to inherit basic control functions, the control class should be derived from the Silverlight system. Windows. Controls. control class. However, it can also

Contentcontrol, itemscontrol, and other control derived classes. Many built-in controls can be derived directly or indirectly from the contentcontrol with the content attribute added, and this attribute allows the control content (such as pressing

Button content. The ListBox control is derived from itemscontrol. itemscontrol can be used to provide users with the basic behavior of controls in the project set. Because we want to implement the button

Contentcontrol is derived.
CodeThe structure is as follows:
Namespace simplebuttondemo
{
Public class simplebutton: contentcontrol
{
}
}

In this case, you have implemented a simple custom control, which can be instantiated by declaration in the XAML document. To illustrate this problem, add the following statements to page. XAML:
<Local: simplebutton/>
To enable Silverlight to recognize this declaration, you also need to add the following attributes to the root usercontrol element of page. XAML:
Xmlns: Local = "CLR-namespace: simplebuttondemo ;"
You can see that CLR-namespace can recognize the namespace in which simplebutton class is defined, whileProgramSet to identify the Assembly containing this control.
The control can be called in XAML. The code structure is as follows:
<Grid X: Name = "layoutroot" background = "white">
<Local: simplebutton/>
</GRID>

Instance:

Naivegradientbutton class

 Using  System;
Using System. windows;
Using System. Windows. controls;
Using System. Windows. Media;

Namespace Naivegradientbuttondemo
{
Public Class Naivegradientbutton: button
{
Gradientstop gradientstop1, gradientstop2;

Public Naivegradientbutton ()
{
Lineargradientbrush brush = New Lineargradientbrush ();
Brush. startpoint = New Point ( 0 , 0 );
Brush. endpoint = New Point ( 1 , 0 );

Gradientstop1 = New Gradientstop ();
Gradientstop1.offset = 0 ;
Brush. gradientstops. Add (gradientstop1 );

Gradientstop2 = New Gradientstop ();
Gradientstop2.offset = 1 ;
Brush. gradientstops. Add (gradientstop2 );

Foreground = Brush;
}

Public Color color1
{
Set {Gradientstop1.color = Value ;}
Get { Return (Color) gradientstop1.color ;}
}

Public Color color2
{
Set {Gradientstop2.color = Value ;}
Get { Return (Color) gradientstop2.color ;}
}
}
}

Add reference in XAML

Xmlns: Local = "CLR-namespace: naivegradientbuttondemo"

 <! -- Layoutroot is the root grid where all page content is placed  -->  
< Grid X: Name = "Layoutroot" Background = "Transparent" >
< Grid. rowdefinitions >
< Rowdefinition Height = "Auto" />
< Rowdefinition Height = "*" />
</ Grid. rowdefinitions >

<! -- Titlepanel contains the name of the application and page title -->
< Stackpanel X: Name = "Titlepanel" Grid. Row = "0" Margin =" >
< Textblock X: Name = "Applicationtitle" Text = "Naivegradientbutton Demo" Style =" {Staticresource phonetextnormalstyle} " />
< Textblock X: Name = "Pagetitle" Text = "Main page" Margin = "9,-7, 0, 0" Style =" {Staticresource phonetexttitle1style} " />
</ Stackpanel >

<! -- Contentpanel-place additional content here -->
< Grid X: Name = "Contentpanel" Grid. Row = "1" Margin = "12, 0, 12, 0" >
< Stackpanel >
< Local: naivegradientbutton Content = "Naive gradient button #1"
Horizontalalignment = "Center" />

< Local: naivegradientbutton Content = "Naive gradient button #2"
Color1 = "Blue" Color2 = "Red"
Horizontalalignment = "Center" />

< Local: naivegradientbutton Content = "Naive gradient button #3"
Color1 =" {Staticresource phoneforegroundcolor} "
Color2 =" {Staticresource phonebackgroundcolor} "
Horizontalalignment = "Center" />

< Local: naivegradientbutton Content = "Naive gradient button #4"
Style =" {Staticresource gradientbuttonstyle} " />
</ Stackpanel >
</ Grid >
</ Grid >
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.