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:
- Open MainPage.xaml.cs.
-
Import the following using statement:
using System.Windows.Browser;
-
Mark the SetItem method with the [ScriptableMemmber] attribute:
[ScriptableMember]
public void SetItem(string url)
{
this.book.Items.Add(url);
}
-
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:
- Open App.xaml.cs.
-
Import the following using statement:
using System.Windows.Browser;
-
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.