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:
- 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 title, string url)
{
this.coverFlow.Items.Add(new SitefinityImage(title, url));
}
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.