Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

HtmlPage.RegisterScriptableObject Method


Registers a managed object for scriptable access by JavaScript code.

Namespace:  System.Windows.Browser
Assembly:  System.Windows.Browser (in System.Windows.Browser.dll)

public static void RegisterScriptableObject(
	string scriptKey,
	Object instance


Type: System.String
The name used to register the managed object.
Type: System.Object
A managed object.


scriptKey or instance is null.


An attempt is made to register a non-public type.


scriptKey contains an embedded null character (\0).


instance has no scriptable entry points.


A property, method, or event that is marked as scriptable uses one of the following reserved names: addEventListener, removeEventListener, constructor, or createManagedObject.

Before you can invoke any managed code from JavaScript, you first need to register the objects you want to expose to the browser. You also need to mark which members are scriptable with ScriptableMemberAttribute.

The following example shows how to mark a single scriptable method in a C# class.

public class Calculator1
    public int Add(int a, int b)
       { return a + b; }

    // Not callable from script.
    public int Subtract(int a, int b)
    { return a – b; }

You can also mark all members at the same time with ScriptableTypeAttribute. When you use the RegisterScriptableObject method, you do not need to mark individual methods as scriptable with ScriptableMemberAttribute.

The following example shows how to mark all methods as callable in a C# class.

public class Calculator2
    public int Add(int a, int b)
       { return a + b; }

    public int Subtract(int a, int b)
    { return a – b; }

Scriptable classes are commonly registered in the App or Page class, as shown in the following example.

public class App : Application
    public App()
      { HtmlPage.RegisterScriptableObject("calc1", new Calculator1());
        HtmlPage.RegisterScriptableObject("calc2", new Calculator2());

JavaScript developers indicate the managed object they want to access by using the scriptKey parameter. The previous example demonstrates how to create the scriptKey values "calc1" and "calc2".

After you have a scriptable object, you can use it from JavaScript, as shown by the following JavaScript code.

void onPluginLoaded(plugin)
    var calc1 = plugin.content.calc1;
    // If you are using ASP.NET AJAX to create a Silverlight instance,
    // use this instead:
    // var calc = plugin.get_element().content.calc1;
    var sum = calc1.Add(5, 1); // Case sensitive.
    try {
        calc.Subtract(5, 1); // Failure. There's no such member.
        } catch (e) {
     alert(e.message ? e.message : e);

If you try to re-register an existing scriptKey, the old registration is removed and is replaced with the new object instance.


Supported in: 5, 4, 3

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Community Additions

© 2015 Microsoft