Share via


HtmlPage.RegisterScriptableObject Method

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Registers a managed object for scriptable access by JavaScript code.

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

Syntax

'Declaration
Public Shared Sub RegisterScriptableObject ( _
    scriptKey As String, _
    instance As Object _
)
public static void RegisterScriptableObject(
    string scriptKey,
    Object instance
)

Parameters

  • scriptKey
    Type: System.String
    The name used to register the managed object.

Exceptions

Exception Condition
ArgumentNullException

scriptKey or instance is nulla null reference (Nothing in Visual Basic).

ArgumentException

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

-or-

scriptKey contains an embedded null character (\0).

-or-

instance has no scriptable entry points.

-or-

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

Examples

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
{
    [ScriptableMember]
    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.

[ScriptableType]
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.
    alert(sum);
    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.

Version Information

Silverlight

Supported in: 5, 4, 3

Platforms

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