Silverlight: When should you use RegisterScriptableObject?

One of the most exciting features of Silverlight 2.0 is its ability to let .NET and JavaScript communicate in a very direct way. JavaScript objects can call .NET methods directly, and register for .NET events.

On the .NET part, this needs a little preparation. There are two steps that you need to perform to enable JavaScript --> .NET communication:

  1. Register the object you want to script.
  2. Mark every scriptable member with the ScriptableAttribute.

The first step lets the web application know that some members must be prepared for JavaScript. Then, each Scriptable member will be fetched and prepared. For example:

[Scriptable] public partial class Page : Canvas { public void Page_Loaded(object o, EventArgs e) { InitializeComponent(); WebApplication.Current.RegisterScriptableObject("MainRoot", this); } [Scriptable] public void DoSomething() { // Do something } }

and:

gslb.SL.TestScriptable.prototype = { handleOnLoad : function(control, userContext, rootElement) { this._control = control; // Set up communication with CLR (if needed) this._rootCanvas = this._control.content.MainRoot; this._rootCanvas.DoSomething(); } }

In the code above, we marked the main Canvas as scriptable, as well as the method DoSomething. Having taken care of registering the main canvas with the WebApplication, we can then access it in JavaScript under the name "MainRoot", and we can call the C# method from the JavaScript code.

One thing is dangerous, however! If you decide to get rid of the method "DoSomething", no scriptable members can be found anymore. In that case, the call to "RegisterScriptableObject" will throw an exception. Any code located after the call to RegisterScriptableObject will not be executed, and it's pretty sure that the application will cease to work correctly. Unfortunately,the error message is not very clear:

Error message 1

If you decide to go on and debug, the error message is not much clearer:

Error message 2

The rule is simple, but one has to remember: You can register a Scriptable Object only if it has Scriptable members!!

Print | posted on Thursday, January 17, 2008 7:51 AM

Feedback

# re: Silverlight: When should you use RegisterScriptableObject?

left by LutayS at 1/17/2008 9:59 PM Gravatar
Hi!
What about performance?

# re: Silverlight: When should you use RegisterScriptableObject?

left by Laurent at 1/17/2008 10:50 PM Gravatar
Hi,

I never tested for performance yet. Given the current state of the development (Silverlight 2.0 is still alpha), I can imagine that it will improve with time. It would especially be interesting to compare the performance of JavaScript --> Silverlight communication as compare to JavaScript --> Java communication. I guess I should reinstall a Java IDE one of these days.

The two areas (to my knowledge) where Silverlight have been tested consistently for performance are:

- UI: http://bubblemark.com/
- Power compared to JavaScript: http://silverlight.net/samples/1.1/chess/run/default.html

HTH,
Laurent

# re: Silverlight: When should you use RegisterScriptableObject?

left by george at 1/18/2008 11:35 AM Gravatar
THANK YOU

# re: Silverlight: When should you use RegisterScriptableObject?

left by ihtesham at 2/2/2008 9:57 PM Gravatar
Hello mate !!

i am having the same problem. the unspecified error.

From your explanation, still i am not sure where to put
the

gslb.SL.TestScriptable.prototype =
{
handleOnLoad : function(control, userContext, rootElement)
{
this._control = control;
// Set up communication with CLR (if needed)
this._rootCanvas = this._control.content.MainRoot;
this._rootCanvas.DoSomething();
}
}
--------------
my c# code is:

[Scriptable]
public partial class Header : Canvas
{
public void Page_Loaded(object o, EventArgs e)
{
// Required to initialize variables
WebApplication.Current.RegisterScriptableObject ("entrypoint", this);

InitializeComponent();
}

[Scriptable]
public void CreateHeader(string width, string height)
{
int w = int.Parse(width);
int h = int.Parse(height);
}
}

and client code is:
var cont = silverlightControl.content.findName("entrypoint");
cont.CreateHeader(w,h);

It gives that ugly unspecified error..

Please help me..

# re: Silverlight: When should you use RegisterScriptableObject?

left by Vinay at 4/18/2008 7:02 AM Gravatar
Hi,

I am using ScriptableObject and it works fine but I get this 'Sys.InvalidOperationException: ManagedRuntimeError # 4002' when the object is changed since it was registered.

What I mean is that I registered an object on a button click, on every subsequent click, I chnage property of this object and register it again (I do unregister before registering it).

For some reason, it doesn't like to register the chnaged object.

Do you guys have any clue?

Cheers
Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification: