Summary The release of Silverlight 2 beta 1 brings us a lot of surprises from runtime and tools, such as supporting the framework languages Visual Basic, Visual C #, ironruby, ironpython, A series of new features such as JSON, Web Service, WCF, and sockets support. The article "one-step learning Silverlight 2 series" takes you to Silverlight 2 development quickly from the following aspects: Silverlight 2 basic knowledge, data and communication, custom controls, animation, and graphic images. Silverlight has built-in support for HTML and client scripts. This article describes how to interact with html dom in Silverlight 2. The first part mainly introduces how to access and modify the existing HTML Dom. We can also create a new Dom element or remove an existing Dom element. In addition, we can also add event processing for DOM elements. Create a DOM element first, let's see how to create a new Dom element. The final result is as follows. When we enter text in the text box, click Create, A Li element is created in the above area. First, define the height of the HTML page and even the Silverlight plug-in.
<div id="parentdiv"> <ul id="list"> </ul></div><div style="height:200px;"> <asp:Silverlight ID="Xaml1" runat="server" Source="~/ClientBin/TerryLee.SilverlightAccessDom2.xap" Version="2.0" Width="100%" Height="200px" /></div>
Define a simple style for the above Div to distinguish it from Silverlight.
#parentdiv{ background:#FCDFB3; border:solid 1px #FF9900; width:500px; height:100px; margin-bottom:20px; }Perform the interface layout in Silverlight. The XAML is as follows:
<Canvas background = "# cdfcae"> <textblock text = "Silverlight accessing the HTML dom" foreground = "red" canvas. top = "10" canvas. left = "30" fontsize = "18"> </textblock> <watermarkedtextbox X: name = "input" watermark = "enter" Height = "40" width = "300" canvas here. left = "30" canvas. top = "50"> </watermarkedtextbox> <button X: name = "createbutton" background = "red" Height = "40" width = "100" content = "CREATE" canvas. top = "50" canvas. left = "350" Click = "createbutton_click"> </button> </canvas>
Compile the code for the create button. First, obtain the parent element of the element to be added to it, that is, the UL defined by us:
HtmlElement parent = HtmlPage.Document.GetElementById("list");
Create a new element Li and Set Properties
HtmlElement child = HtmlPage.Document.CreateElement("li");child.SetAttribute("innerText", this.input.Text);Add new elements to parent
parent.AppendChild(child);
The complete code is as follows:
private void createButton_Click(object sender, RoutedEventArgs e){ HtmlElement parent = HtmlPage.Document.GetElementById("list"); HtmlElement child = HtmlPage.Document.CreateElement("li"); child.SetAttribute("innerText", this.input.Text); parent.AppendChild(child);}After running the command, create the first element and create a new one to remove the DOM element. Now that you can create an element, you can also delete the corresponding element. We put several <li> elements on the page, then enter the ID to delete the file. The Code is as follows:
private void deleteButton_Click(object sender, RoutedEventArgs e){ HtmlElement parent = HtmlPage.Document.GetElementById("list"); HtmlElement child = HtmlPage.Document.GetElementById(this.input.Text); parent.RemoveChild(child);}After running, three <li> events are displayed on the page. In addition to adding and removing DOM elements, you can also add events to and from DOM elements, in the following example, a DOM element <A> is dynamically created using Silverlight without registering a click event. when clicked, the rectangular background color in Silverlight is modified. First, prepare the relevant HTML.
<div id="parent"></div><div style="height:200px;"> <asp:Silverlight ID="Xaml1" runat="server" Source="~/ClientBin/TerryLee.SilverlightAccessingDom3.xap" Version="2.0" Width="100%" Height="200px" /></div>
Two styles are defined. newstyle is used for the newly added a element in Silverlight.
#parent{ background:#FCDFB3; border:solid 1px #FF9900; width:500px; height:100px; margin-bottom:20px; }.newstyle{ background:#0099FF; border:solid 1px #0000FF;}Make a simple interface and place a button and rectangle:
<Canvas background = "# cdfcae"> <textblock text = "Silverlight accessing the HTML dom" foreground = "red" canvas. top = "10" canvas. left = "30" fontsize = "18"> </textblock> <rectangle X: name = "result" Height = "40" width = "300" fill = "red" canvas. left = "30" canvas. top = "50" radiusx = "5" radiusy = "5"> </rectangle> <button X: name = "addbutton" background = "red" Height = "40" width = "100" content = "" canvas. top = "50" canvas. left = "350" Click = "addbutton_click"> </button> </canvas>
Add a DOM element, create an A element, and set attributes for it. cssclass is used to define its style:
Htmlelement parent = htmlpage. document. getelementbyid ("parent"); htmlelement button = htmlpage. document. createelement ("A"); button. setattribute ("innertext", "change color in Silverlight"); button. setattribute ("href", "#"); button. cssclass = "newstyle"; parent. appendchild (button );
Append an onclick event to element A. htmlelement provides the attachevent method to append events. With the generic eventhandler, we change the rectangle fill color and border in Silverlight when element a is clicked.
button.AttachEvent("onclick", new EventHandler<HtmlEventArgs>(button_Click));
void button_Click(object sender, HtmlEventArgs e){ result.Stroke = new SolidColorBrush(Colors.Black); result.Fill = new SolidColorBrush(Colors.Green); result.StrokeThickness = 2;}
The complete code is as follows:
Private void addbutton_click (Object sender, routedeventargs e) {htmlelement parent = htmlpage. document. getelementbyid ("parent"); htmlelement button = htmlpage. document. createelement ("A"); button. setattribute ("innertext", "change color in Silverlight"); button. setattribute ("href", "#"); button. cssclass = "newstyle"; parent. appendchild (button); button. attachevent ("onclick", new eventhandler
Run the following command to check the effect. The detachevent method is also provided to add the new element a on the starting interface and click to change the background color of the rectangle to cancel event registration. This article briefly introduces how to add and remove DOM elements in Silverlight, and how to add and cancel event handlers for DOM elements.
This article is from the "terrylee technology column" blog, please be sure to keep this source http://terrylee.blog.51cto.com/342737/67260
This article is from 51cto. com technical blog