This is a mirror of official site: http://jasper-net.blogspot.com/

How to implement communication between Silverlight and the HTML host.

| Monday, January 30, 2012
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.

    Create a class called JavaScriptBridge
    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.

    [ScriptableMember()]
    public void DoSomething(int a, int b)
    {

    }


    Inside the App.xaml.cs, on the Application_startup event handler, register the an instance of the bridge

            private void Application_Startup(object sender, StartupEventArgs e)
            {
                this.RootVisual = new MainPage();
                JavaScriptBridge javaScriptBridge = new JavaScriptBridge();

                HtmlPage.RegisterScriptableObject("bridge", javaScriptBridge);


            }


    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" />


Read more: Ariel's Remote Data Center
QR: how-to-implement-communication-between-silverlight-and-the-html-host.aspx

Posted via email from Jasper-net

0 comments: