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:

   1: <UserControl x:Class="SilverlightApplication7.Page"
   2:     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
   3:     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
   4:     Width="400" Height="300">
   5:     <Grid x:Name="LayoutRoot" Background="White">
   6:         <TextBlock x:Name="myTextbox">Waiting for call...</TextBlock>
   7:     </Grid>
   8: </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