Making the Silverlight classes and methods scriptable

In this tutorial you must make the SetItem method and the instance of the MainPage class accessible from javascript. To access methods and object instances in the Silverlight application from javascript, you must mark them as scriptable.

To mark the SetItem method as scriptable, perform the following:

  1. Open MainPage.xaml.cs.
  2. Import the following using statement:

    using System.Windows.Browser;

  3. Mark the SetItem method with the [ScriptableMemmber] attribute:

    [ScriptableMember]
    public void SetItem(string title, string url)
    {
        this.coverFlow.Items.Add(new SitefinityImage(title, url));
    }

To register the MainPage instance as scriptable, perform the following:

  1. Open App.xaml.cs.
  2. Import the following using statement:

    using System.Windows.Browser;

  3. Edit the code in the Application_Startup handler as follows:

    private void Application_Startup(object sender, StartupEventArgs e)
    {
        var mainPage = new MainPage();
        this.RootVisual = mainPage;
     
        HtmlPage.RegisterScriptableObject("mainPage", mainPage);
    }

For more information about scriptable elements in Silverlight, see Making Silverlight Scriptable by JavaScript.

Related topics:

Feedback

How useful is this article?

Tell us more

Submit
Your message was successfully sent.

We appreciate your feedback.

Your message could not be sent.

OK