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
HtmlPage.BrowserInformation.Name or HtmlPage.BrowserInformation.CookiesEnabled
To find element in html code, for example <div id="findMe" /> use method GetElementById(..), like in example
HtmlElement _div = _htmlDocument.GetElementById("findMe");
Now, you can do everything with your element. For example change/add/remove style
_div.SetStyleAttribute("color", "#FF0000");
etc.
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/>
</form>
find him, and add event:
_htmlDocument.GetElementById("htmlButton ").AttachEvent("onclick", new
EventHandler<HtmlEventArgs>(this.OnHtmlButtonClick));
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)
http://jacekciereszko.pl/2008/04/ping-pong-with-html-and-silverlight-20.html
Source code: http://wpierdalaj.pl/blog_resources/new/HtmlPingPong.zip
Demo
Jacek Ciereszko