The XAML Tag Element is converted to the corresponding object during the Silverlight runtime. You can use the load method of the xamlreader class to dynamically create the UI element:
- Specify a XAML content string to run according to XML rules. xamlreader. Load () Now you need to specify an xmlns in your XAML file;
- Use the xamlreader. Load Method to compile the elements in the memory (in this way, you can obtain the reference of the UI element object, which may be null or an error is reported );
- Add it to the sub-control of the container.
Next we will make a simple clock. Page. XAML is as follows:
<Usercontrol X: class = "openxmlvideo2.page"
Xmlns = "http://schemas.microsoft.com/client/2007"
Xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml"
Width = "187" Height = "97">
<Canvas X: Name = "eclock" Height = "97" width = "187">
</Canvas>
</Usercontrol>
Page. XAML. CS:
Using System;
Using System. Collections. Generic;
Using System. LINQ;
Using System. windows;
Using System. Windows. controls;
Using System. Windows. documents;
Using System. Windows. input;
Using System. Windows. Media;
Using System. Windows. Media. animation;
Using System. Windows. shapes;
Using System. Windows. markup;
Namespace Openxmlvideo2
{
Public Partial Class Page: usercontrol
{
Private Textblock textblock1;
Private System. Windows. Threading. dispatchertimer timer;
Public Page ()
{
Initializecomponent ();
This. Loaded+ = NewRoutedeventhandler (page_loaded );
}
Void Page_loaded ( Object Sender, routedeventargs E)
{
String XAML = String . Empty;
XAML = " <Textblock xmlns = \ " HTTP: // Schemas.microsoft.com/client/2007\ "margin = \" 14,11, \ "name = \" textblock1 \ "fontfamily = \" time new Roman \ "fontsize = \" 40 \ "> 00:00:00 </textblock> ";
Textblock1 = Xamlreader. Load (XAML) As Textblock;
// Loaded is the loading event of textblock, And the textblockreceivloaded in it is the name of the event handler.
Textblock1.loaded + = New Routedeventhandler (textblockreceivloaded );
// The setvalue method must be used to change the Attached properties.
Textblock1.setvalue (canvas. leftproperty, 2 );
Textblock1.setvalue (canvas. topproperty, 2 );
// Add textblock1 as a sub-object to the canvas (similar to the Control tree on the Asp.net page)
This . Eclock. Children. Add (textblock1 );
}
Void Textblockreceivloaded ( Object Sender, routedeventargs E)
{
// When dispatchertimer is used, I set the interval to 1 second. The timer interval event is also a tick event.
Timer = New System. Windows. Threading. dispatchertimer ();
Timer. Interval = New Timespan ( 0 , 0 , 1 ); // Interval: 1 second
Timer. tick + = New Eventhandler (timer_tick );
Timer. Start ();
}
Void Timer_tick ( Object Sender, eventargs E)
{
Textblock1.text=Datetime. Now. tolongtimestring ();
}
}
}
The running result is as follows:
A simple electronic clock is ready. The main learning method is to dynamically create the UI element and dispatchertimer through the load method of the xamlreader class.
References:
Changes in Silverlight 2 (breaking changes in Silverlight 2 ):
Http://www.cnblogs.com/worksguo/archive/2008/03/07/1094347.html
Timer class dispatchertimer in silverlight2.0 -- no longer use storyboard Timer
Http://www.cnblogs.com/gowhere/archive/2008/03/11/silverlight2_dispatchertimer.html