New Features of Silverlight 5 beta Exploration Series: 8. Custom extension tag in Silverlight 5

Source: Internet
Author: User

The custom extension tag is added in Silverlight 5, which inherits fromMarkupextensionClass, reload the providevalue method in this class to determine the corresponding return value, to set the property of the bound control.

Next we will use the simplest example to understand how the custom extension tag works.

Step 1: Create a usermarkextension. CS class. Note that usermark (extension Tag Name) + extension. CS (fixed suffix) = usermarkextension. CS

Step 2: set three accessible attribute labels lbtext, lbwidth, and rcradius

Step 3: Reload the prividevalue function and set the returned property value,

Usermarkextension. CS class in three stepsCodeAs follows:

   ///    <Summary>  
/// Step 1: Create a usermarkextension. CS class
/// Note: usermark (extension Tag Name) + extension. CS (fixed suffix) = usermarkextension. CS
/// </Summary>
Public Class Usermarkextension: markupextension
{
// Step 2: set three accessible attributes lbtext, lbwidth, and rcradius
Public String Lbtext { Get ; Set ;}
Public Double Lbwidth { Get ; Set ;}
Public Double Rcradius { Get ; Set ;}
// Step 3: Reload the prividevalue function and set the returned property value
Public Override Object Providevalue (iserviceprovider serviceprovider)
{
// If the lbtext value is text when the foreground calls a custom tag,
If (Lbtext = " Text " )
{
Return Lbtext = " I am a text " ;
}
Else If (Rcradius > 0 )
{
Return Rcradius = 15 ;
}
Else
{
Return Lbwidth = 99 ;
}

}
}

Step 4: set {local: usermark lbtext = text} For the control that needs to set the custom extension tag. The meaning is to set the custom extension tag local: usermark, the value of its attribute lbtext is text. Based on this value, you can query the prividevalue function in the Custom extension tag. Therefore, the returned value is "I am a text ". Similarly, other property values are bound! The mainpage. XAML code is as follows:

 
   
    
    
    
    
   

In this example, we only briefly demonstrate how to use and run custom extension labels. I personally think this function will be very helpful for mvvm support, you can try to extend it.

This example is written in vs2010 + Silverlight 5.0 Beta. To download the source code, click sl5markupext.zip. The following shows how to run:

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.