Geeks With Blogs
Light Up the Web Blog about programming in Silverlight
I created demo that show how to connect Silverlight 2.0 with code html. This demo is like a game ping-pong, but it is not really game because you not suppose to play but learn Silverlight 2.0 !

To connetct from Silverlight 2.0 to HTML you should

Get html document
HtmlDocument _htmlDocument = HtmlPage.Document;

, where
HtmlPage is from System.Windows.Browser;

From HtmlPage object you can get other informations, like

or HtmlPage.BrowserInformation.CookiesEnabled

To find element in html code, for example
<div id="findMe" /> use method GetElementById(..), like in example

_div = _htmlDocument.GetElementById("findMe");

Now, you can do everything with your element. For example change/add/remove style

_div.SetStyleAttribute("color", "#FF0000");


To connetct from HTML to Silverlight 2.0 you should

To call Silverlight, make Button in HTML:

form action="">

<input type="button" id="htmlButton" value="Push me! I'm in Html"/><br/>


find him, and add event:

_htmlDocument.GetElementById("htmlButton ").AttachEvent("onclick", new

and method called by button
publicvoid OnHtmlButtonClick(object sender, HtmlEventArgs args)

ScriptObjectCollection htmlElements = _htmlDocument.GetElementsByTagName("findMeElementName");
foreach (HtmlElement h1Element in htmlElements)


h1Element.SetStyleAttribute("color", "#FFFF00");



My demo from my polish blog (in polish)

Source code:


Jacek Ciereszko

Posted on Monday, March 31, 2008 12:54 PM | Back to top

Copyright © Jacek | Powered by: