HtmlPage..::.RegisterScriptableObject Method
This page is specific to:Microsoft Version:Silverlight 3
.NET Framework Class Library for Silverlight
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)
Syntax

'Usage

Dim scriptKey As String
Dim instance As Object

HtmlPage.RegisterScriptableObject(scriptKey, _
    instance)

'Declaration

Public Shared Sub RegisterScriptableObject ( _
    scriptKey As String, _
    instance As Object _
)

Parameters

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

ExceptionCondition
ArgumentNullException

scriptKey or instance is nullNothingnullptra 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.

Platforms

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

See Also

Reference

© 2009 Microsoft Corporation. All rights reserved.   Terms of Use | Trademarks | Privacy Statement
Page view tracker
Rate the Lightweight library
x
Lightweight builds on ScriptFree (loband) by adding features you've requested: a SearchBox and default code language selection.
Do you like the SearchBox?
Do you like the tabbed code blocks?
How useful is this topic?
Tell us more.
Thanks
x
You're helping to improve MSDN Online.
Feedback
Switch View
Classic
Lightweight Beta
ScriptFree
Switch View