<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Rexxars.com &#187; Silverlight</title>
	<atom:link href="http://rexxars.com/tag/silverlight/feed/" rel="self" type="application/rss+xml" />
	<link>http://rexxars.com</link>
	<description>A developers perspective on PHP, Android, Mootools, Node.JS and other awesomeness.</description>
	<lastBuildDate>Tue, 08 Nov 2011 15:33:11 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.1</generator>
		<item>
		<title>Calling Javascript from Silverlight</title>
		<link>http://rexxars.com/silverlight/calling-javascript-from-silverlight/</link>
		<comments>http://rexxars.com/silverlight/calling-javascript-from-silverlight/#comments</comments>
		<pubDate>Thu, 06 May 2010 20:49:54 +0000</pubDate>
		<dc:creator>Rexxars</dc:creator>
				<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://rexxars.com/?p=120</guid>
		<description><![CDATA[Over the past month or two, I&#8217;ve been learning Silverlight at work, so I might put up a few posts when I encounter interesting topics. The solutions I post might not be the best way of doing things, obviously Integrating Silverlight into a rich, Javascript-driven website, I found myself wanting to call Javascript functions from [...]]]></description>
			<content:encoded><![CDATA[<p>Over the past month or two, I&#8217;ve been learning Silverlight at work, so I might put up a few posts when I encounter interesting topics. The solutions I post might not be the best way of doing things, obviously <img src='http://rexxars.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
<p>Integrating Silverlight into a rich, Javascript-driven website, I found myself wanting to call Javascript functions from Silverlight. A quick Google search told me this was possible, using the <a href="http://msdn.microsoft.com/en-us/library/system.windows.browser.scriptobject.invoke%28v=VS.95%29.aspx">HtmlPage.Window.Invoke</a> method. You could also use the Eval method, but it feels like a dirty hack,<br />
so let&#8217;s not go there.</p>
<pre lang="csharp" line="1">
private void displayDialog(string message)
{
	HtmlPage.Window.Invoke("showSomeDialog", message);
}
</pre>
<p>Easy, right? But like a good Javascript-coder, I like to &#8220;namespace&#8221; my methods, instead of leaving lots of global functions around. The invoke method seems to call global objects only, so I can&#8217;t do <strong>Invoke(&#8220;Rexxars.showSomeDialog&#8221;, &#8220;message&#8221;)</strong> &#8211; which is a shame. So I thought to myself, I&#8217;ll just create a &#8220;proxy&#8221; function which calls methods in my namespace. Simple:</p>
<pre lang="javascript" line="1">
function slBridge(method, arg) {
	if(Rexxars[method]) {
		Rexxars[method](arg);
	}
}
</pre>
<p>Now I can do: <strong>HtmlPage.Window.Invoke(&#8220;slBridge&#8221;, &#8220;showSomeDialog&#8221;, message);</strong> &#8211; cool. But what if I want to call a function with more than one argument? I further improved my Silverlight-Javascript &#8220;bridge&#8221; to support a variable number of arguments:</p>
<pre lang="javascript" line="1">
function slBridge() {
	// Turn the arguments object into a regular, usable array
	var args = Array.prototype.slice.call(arguments);
	// Method name is the first argument and should not be included
	var method = args.shift();
	// Make sure the method exists inside our namespace
	if(Rexxars[method]) {
		// Call the method with the arguments passed
		Rexxars[method].apply(null, args);
	}
}
</pre>
<p>Simple, eh? Now I can use the slBridge function to call any method inside my Rexxars namespace, with different number of arguments in each. Sweet! <img src='http://rexxars.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://rexxars.com/silverlight/calling-javascript-from-silverlight/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

