DCSIMG
How to implement communication between Silverlight and the HTML host. - Ariel's Remote Data Center

How to implement communication between Silverlight and the HTML host.

Posted Jan 29 2012, 11:41 AM by Ariel Ben Horesh  

A question about intercommunication between Silverlight and the HTML host has been asked in the Israeli MSDN forum.

Since I’ve already implemented it once in a project, I believe I can extract the great info already exist in the MSDN documentation to a more direct how-to.

Let’s begin.

  1. Create a class called JavaScriptBridge
  2. Each method that you would like to be exposed to the HTML host, thus be possible to get called by JavaScript you adorn with [ScriptableMember] attribute.
  3. [ScriptableMember()]
    public void DoSomething(int a, int b)
    {

    }

  4. Inside the App.xaml.cs, on the Application_startup event handler, register the an instance of the bridge
  5.         private void Application_Startup(object sender, StartupEventArgs e)
    {
    this.RootVisual = new MainPage();
    JavaScriptBridge javaScriptBridge = new JavaScriptBridge();

    HtmlPage.RegisterScriptableObject("bridge", javaScriptBridge);


    }

  6. We are done with Silverlight side, now we move on to the HTML host, locate the aspx file (usually) that contains the Silverlight object. In this page you will notice that it is represented as an <object> tag. Insert an event handler for the onLoad event of the object.
    <param name="onLoad" value="pluginLoaded" />
  7. Also add an input button for testing the interface.
      <input id="Button1" type="button" value="Test" 
    onclick="test()" />
  8. Implement both event handlers:
         var silverlight = null;
    function pluginLoaded(sender, args) {
    silverlight = sender.getHost();
    }

    function test() {
    if (silverlight != null) {
    silverlight.Content.bridge.DoSomething(10, 10);
    }
    }
  9. Hopefully, now we look back and appreciate how easy it is to mix those two worlds Smile

Ariel


Comments

How to implement communication between Silverlight and the HTML host. « Mas-Tool's Favorites said:

Pingback from  How to implement communication between Silverlight and the HTML host. &laquo;  Mas-Tool&#039;s Favorites

# January 29, 2012 2:05 PM

1 said:

-1'

# January 30, 2012 3:45 AM

Dew Drop – January 29, 2012 (#1,253) | Alvin Ashcraft's Morning Dew said:

Pingback from  Dew Drop &ndash; January 29, 2012 (#1,253) | Alvin Ashcraft&#039;s Morning Dew

# January 30, 2012 4:35 AM

Leave a Comment

(required) 

(required) 

(optional)

(required) 


Enter the numbers above: