Making the Silverlight classes and members scriptable

In this tutorial you must make the SetItem and the SetBookProperties methods 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 methods 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 url)
    {
        this.book.Items.Add(url);
    }
  4. Mark the SetBookProperties method with the [ScriptableMemmber] attribute:

    [ScriptableMember]
    public void SetBookProperties(dynamic props)
    {
        try
        {
            this.book.HardPages = Enum.Parse(typeof(Telerik.Windows.Controls.HardPages), props.hardPages, true);
            this.book.FirstPagePosition = Enum.Parse(typeof(Telerik.Windows.Controls.PagePosition), props.firstPagePosition, true);
            this.book.PageFlipMode = Enum.Parse(typeof(Telerik.Windows.Controls.PageFlipMode), props.pageFlipMode, true);
            this.book.RightPageIndex = Convert.ToInt32(props.rightPageIndex.ToString());
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

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.

Next steps

+1-888-365-2779
sales@sitefinity.com

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