<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Matt Manela's Blog : Silverlight</title><link>http://blogs.msdn.com/matt/archive/tags/Silverlight/default.aspx</link><description>Tags: Silverlight</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>Combining Silverlight and JavaScript</title><link>http://blogs.msdn.com/matt/archive/2008/04/13/combining-silverlight-2-and-javascript.aspx</link><pubDate>Mon, 14 Apr 2008 02:49:46 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8389220</guid><dc:creator>MattManela</dc:creator><slash:comments>2</slash:comments><comments>http://blogs.msdn.com/matt/comments/8389220.aspx</comments><wfw:commentRss>http://blogs.msdn.com/matt/commentrss.aspx?PostID=8389220</wfw:commentRss><description>&lt;p&gt;&lt;a href="http://www.microsoft.com/silverlight/resources/install.aspx?v=2.0"&gt;Silverlight 2&lt;/a&gt; is currently in Beta 1 but even in this early stage it has many amazing features.&amp;nbsp; One of these features which I was toying around with today was its ability to integrate with its host page's &lt;a href="http://www.w3.org/TR/DOM-Level-2-Core/introduction.html"&gt;DOM&lt;/a&gt;.&amp;nbsp; What this means is that from Silverlight you are able to call Javascript functions and from JavaScript you can call Silverlight methods.&lt;/p&gt; &lt;p&gt;To demonstrate this feature I created a simple but interesting sample:&lt;/p&gt; &lt;p&gt;&lt;a href="http://silverlight.jemts.com/SL2JS/" target="_blank"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="365" alt="image" src="http://blogs.msdn.com/blogfiles/matt/WindowsLiveWriter/CombiningSilverlight2andJavaScript_D722/image_5.png" width="444" border="0"&gt;&lt;/a&gt;&amp;nbsp; &lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;This &lt;a href="http://silverlight.jemts.com/SL2JS/" target="_blank"&gt;webpage&lt;/a&gt; which can be found &lt;a href="http://silverlight.jemts.com/SL2JS/" target="_blank"&gt;here&lt;/a&gt; contains two areas, a JavaScript area and a Silverlight area.&amp;nbsp; Each has a small square in it.&amp;nbsp; When you move the square in one area it updates its position in the other.&amp;nbsp; The beauty of this is how simple it is to make this work.&amp;nbsp; I will show how I update the squares position from Silverlight and from Javascript:&lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Calling Javascript Functions from Silverlight&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;This is the simpler of the two.&amp;nbsp; In Silverlight under the &lt;strong&gt;System.Windows.Browser&lt;/strong&gt; namespace there is an object called HtmlPage.&amp;nbsp; This object gives you access to the pages DOM.&amp;nbsp; The code I use to move the square in the JavaScript area is:&lt;/p&gt; &lt;div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4"&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; height: 29px; background-color: #f4f4f4; border-bottom-style: none"&gt;HtmlPage.Window.Invoke(&lt;span style="color: #006080"&gt;"moveBox"&lt;/span&gt;, newX, newY);&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;where "moveBox" is the name of a Javascript method I wrote to update the red square's position.&lt;/p&gt;
&lt;p&gt;That is all I have to do!&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Calling Silverlight Methods from Javascript&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;This is slightly more complicated. First in the Silverlight application I have to mark which class I want to expose to JavaScript as by giving it a &lt;strong&gt;Scriptable &lt;/strong&gt;attribute: &lt;/p&gt;
&lt;div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, 'Courier New', courier, monospace; height: 58px; background-color: #f4f4f4"&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;[ScriptableType]
&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;partial&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; Page : UserControl&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Then I need to register this class so that JavaScript can see it and give it a name for JavaScript to refer to it as:&lt;/p&gt;
&lt;div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, 'Courier New', courier, monospace; height: 49px; background-color: #f4f4f4"&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;HtmlPage.RegisterScriptableObject(&lt;span style="color: #006080"&gt;"silverlightMove"&lt;/span&gt;, &lt;span style="color: #0000ff"&gt;this&lt;/span&gt;);&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Next I need to mark which methods to expose to JavaScript by giving them a&amp;nbsp; &lt;strong&gt;ScriptableMember&lt;/strong&gt; attribute&lt;strong&gt;.&amp;nbsp; &lt;/strong&gt;Here I am exposing a method called MoveBox which updates the blue square's position&amp;nbsp; in the Silverlight application:&lt;/p&gt;
&lt;div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4"&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; height: 69px; background-color: #f4f4f4; border-bottom-style: none"&gt;[ScriptableMember]
&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; MoveBox(&lt;span style="color: #0000ff"&gt;int&lt;/span&gt; x, &lt;span style="color: #0000ff"&gt;int&lt;/span&gt; y)&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;With all this setup done I can now move back to JavaScript and get ready to call the method.&amp;nbsp; To do this I need to get a reference to the pages Silverlight control:&lt;/p&gt;
&lt;div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, 'Courier New', courier, monospace; height: 61px; background-color: #f4f4f4"&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;silverLightControl = document.getElementById(&lt;span style="color: #006080"&gt;"silverlightControl"&lt;/span&gt;);&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Where "silverlightControl" is the id&amp;nbsp; applied to my Silverlight application object tag.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Once I have a reference to the Silverlight control I am able to invoke the method which I marked as a ScriptableMember using the object name I registered:&lt;/p&gt;
&lt;div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4"&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; height: 45px; background-color: #f4f4f4; border-bottom-style: none"&gt;silverLightControl.content.silverlightMove.MoveBox(x, y);&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;I have now called MoveBox method from my C# code in the Silverlight application using Javascript!&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;The code for this sample can be found at &lt;a href="http://code.msdn.microsoft.com/SilverlightDOM" target="_blank"&gt;here at MSDN Code Gallery&lt;/a&gt;.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8389220" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/matt/archive/tags/Programming/default.aspx">Programming</category><category domain="http://blogs.msdn.com/matt/archive/tags/JavaScript/default.aspx">JavaScript</category><category domain="http://blogs.msdn.com/matt/archive/tags/Silverlight/default.aspx">Silverlight</category></item></channel></rss>