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: