Click to Rate and Give Feedback
MSDN
MSDN Library
Web Development
Silverlight 3
HtmlPage Class
HtmlPage Methods
 RegisterScriptableObject Method

  Switch on low bandwidth view
.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)
Visual Basic (Declaration)
Public Shared Sub RegisterScriptableObject ( _
    scriptKey As String, _
    instance As Object _
)
Visual Basic (Usage)
Dim scriptKey As String
Dim instance As Object

HtmlPage.RegisterScriptableObject(scriptKey, _
    instance)
C#
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 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.

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.

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

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker