Page view counter

Silverlight Tips of the Day - Blog by Mike Snow

Game Programming with Silverlight

Silverlight Tip of the Day #15 – Communicating between JavaScript & Silverlight

Communicating between Javascript and Silverlight is, fortunately, relatively straight forward. The following sample demonstrates how to make the call both ways.

Calling Silverlight From Java script:

  1. In the constructor of your Silverlight app, make a call to RegisterScriptableObject().This call essentially registers a managed object for scriptable access by JavaScript code. The first parameter is any key name you want to give. This key is referenced in your Javascript code when making the call to Silverlight.
  2. Add the function you want called in your Silverlight code. You must prefix it with the [ScriptableMember] attribute.
  3. In Javascript, you can now call directly into your Silverlight function. This can be done through the document object. From my example below: document.getElementById("silverlightControl").Content.Page.UpdateText("Hello from Javascript!"); where “silverlightControl” is the ID of my Silverlight control.

Calling Javascript From Silverlight:

  1. Javascript can be directly called via the HtmlPage.Window.Invoke() function.

Example for both:

Page.xaml:

namespace SilverlightApplication
{
    public partial class Page : UserControl
    {
        public Page()
        {
            InitializeComponent();
 
            HtmlPage.RegisterScriptableObject("Page", this);            
            HtmlPage.Window.Invoke("TalkToJavaScript", "Hello from Silverlight");
        }
 
        [ScriptableMember]
        public void UpdateText(string result)
        {
            myTextbox.Text = result;
        }
    }
} 

Default.aspx:

<script type="text/javascript"> 
        function TalkToJavaScript( data)
        {
            alert("Message received from Silverlight: "+data);
            
            var control = document.getElementById("silverlightControl");            
            control.Content.Page.UpdateText("Hello from Javascript!");
        }    
</script>

Page.xaml:

<UserControl x:Class="SilverlightApplication7.Page"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Width="400" Height="300">
    <Grid x:Name="LayoutRoot" Background="White">
        <TextBlock x:Name="myTextbox">Waiting for call...</TextBlock>
    </Grid>
</UserControl>

Thank you,
--Mike Snow

 Subscribe in a reader

Comments

Microsoft Weblogs said:

Communicating between Javascript and Silverlight is, fortunately, relatively straight forward. The following

# July 8, 2008 11:09 AM

Silverlight and JavaScript | DavideZordan.net said:

Pingback from  Silverlight and JavaScript | DavideZordan.net

# July 8, 2008 2:26 PM

Community Blogs said:

Martin Mihaylov on the MultiscaleImage control, Tamir Khason on Visual Tree and thickness, Mike Snow

# July 9, 2008 2:21 AM

Dew Drop - July 9, 2008 | Alvin Ashcraft's Morning Dew said:

Pingback from  Dew Drop - July 9, 2008 | Alvin Ashcraft's Morning Dew

# July 9, 2008 11:08 AM

Mirrored Blogs said:

Post: Approved at: Jul-9-2008 Using Silverlight to Create a Video Player http://michaelsync.net/2008

# July 9, 2008 4:17 PM

Visual Web Developer Team Blog said:

6 new Silverlight tutorials are completed! Tip of the Day #15 - Communicating between JavaScript &amp;

# July 17, 2008 5:19 PM

SilverlightBlog.com said:

JavaScript

# July 24, 2008 7:06 PM

Silverlight - Tip of the Day by Mike Snow at Blog von J??rgen Ebner said:

Pingback from  Silverlight - Tip of the Day by Mike Snow at Blog von J??rgen Ebner

# July 28, 2008 7:06 AM

Silverlight Tips of the Day - Blog by Mike Snow said:

The purpose of this post is to create an outline summary all the blogs from my Silverlight tips of the

# January 2, 2009 5:56 PM

o UAU nosso de cada dia said:

essa lista eu copiei desse blog bárbaro (acompanhe por RSS você também): uma lista de dicas super úteis

# January 3, 2009 6:25 AM

jin_u as blog » Blog Archive » ??????????????? ??? ?????? said:

Pingback from  jin_u as blog  &raquo; Blog Archive   &raquo; ??????????????? ??? ??????

# January 14, 2009 8:57 PM

ArneHB said:

document.getElementById is the id you find in the aspx/html file where the Silverlight is called. Took a while before I figured this out.

Example: (In SilverlightAppTestPage.aspx - The statup page)

<asp:Silverlight ID="Xaml1" runat="server" Source="~/ClientBin/SilverlightApp.xap" MinimumVersion="2.0.31005.0" Width="100%" Height="100%" />

Then it should say: var control = document.getElementById("Xaml1");  because Xaml1 is the id to the silverlight as said above.

Other then that I like this tip very much, simple and easy :)

# May 11, 2009 7:48 AM

mike.snow said:

Good point, I always just my ID to make more sense but I should call that out. Thanks.

# May 11, 2009 10:36 AM

jigs_jignesh said:

really helpful and nice artical.....

# November 2, 2009 2:37 AM