Communicating between two Silverlight objects on a single page

Date July 8, 2008 @ 1:35 am in Microsoft, Silverlight

Not sure where I stumbled on it, but recently I watched video online where as the video was playing, a banner ad on the same page changed as the context of the video changed.  I was intrigued by the potential this technique had in interactive marketing.  As I was watching the video I was envisioning a character picking up a can of Pepsi and at that moment a corresponding banner ad somewhere else on the page switching to an ad for Pepsi.  This could be a very powerful way to create interactive calls to action.

This got me thinking about how I might duplicate that technique in Silverlight 2. I figured that the first step was to figure out a simple way communicate between two distinct Silverlight object which exists on the same page.  For this example I am going to have one SL object contain a series of Buttons, while the other SL object contains a single TextBlock.

slobjects_1


In the image above the red box I refer to as slObject1, where the blue box I refer to as slObject2.  Both of these <objects> have unique id’s and point to their own respective Silverlight Project and both have been placed in a single html file. 

In this example my slObject1 will be executing a Javascript function, which in turn will be calling an internal function inside of slObject2.  To do this I simply add a reference to the “System.Windows.Browser” namespace and call the Invoke method of the Window object.  here is what my button click handler looks like. 

slobjects_2


Once I have this defined I can now go back into my default.html file and define this Javascript function.  The function finds the appropriate html object and calls the internal function in slObject2

slobjects_3


To create my “UpdateTextBlock function I need to add a few things to the slObject2 project.  First I have to go into my App.xaml.cs file and enter the following in my Application_Startup handler.  “HtmlPage.RegisterScriptableObject("Page", page);”  This registers a managed object for scriptable access by JavaScript code.  Once registered I can create my methods as follows.

slobjects_4


That is all there is to it.  Now that I have the ability to communicate between the two SL object I can go after a more complicated implementation.

Code: SLObjects.zip

/p>

2 Responses to “Communicating between two Silverlight objects on a single page”

  1. Suvik said:

    And how actually be whith animation? =(

  2. » Silverlight Video Player updated to display external bookmarks said:

    [...] feature.  I am excited someone really wants it.  Back in July I created a post called Communicating between two Silverlight objects on a single page. In this post I demonstrate how one can use a [ScriptableMember] to expose a method to JavaScript. [...]

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">