Export (0) Print
Expand All
0 out of 3 rated this helpful - Rate this topic

HtmlPage.RegisterScriptableObject Method

Silverlight

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
)

Parameters

scriptKey
Type: System.String
The name used to register the managed object.
instance
Type: System.Object
A managed object.
ExceptionCondition
ArgumentNullException

scriptKey or instance is null.

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.

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.

Silverlight

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.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.