(X) Hide this SilverlightShow next training events: Secure & Personalize Your Silverlight App with WCF RIA Services. May 25th, 10 am PST. Sign up
XNA for Windows Phone 7 - a 3 day training by MCC Peter Kuhn. June 1-3, 9 am - 1.30 pm PST. Sign up early-bird for $199.
Skip Navigation LinksHome / Articles / View Article

Silverlight 2 - RegisterCreateableType

+ Add to SilverlightShow Favorites
0 comments   /   aggregated from Mike Taulty's Blog on Apr 28, 2008  /  original article
(0 votes)
Categories: Tips and Tricks

I spent about 2 hours trying to figure this out today so I thought I'd just share it here. Mostly, the time lost comes down to Javascript but that's no big surprise :-(

Essentially, I'm playing with the HTML Bridge that lets Silverlight .NET code call Javascript code and vice versa. It's all working very smoothly until I want to create an instance of a .NET type ( marked as ScriptableType ) from Javascript.

I know that somewhere, there's a method that may be called;

createObject

createManagedObject

createManagedInstance

but I can't figure out what the heck it's called and I can't figure out a decent way of figuring that out :-)

So...with Silverlight 2 Beta 1 I can confirm that if you have a .NET type such as;

  [ScriptableType]
  public class Point
  {
    [ScriptableMember]
    public int X { get; set; }
    [ScriptableMember]
    public int Y { get; set; }
  }

and if you've registered this for use from Javascript at some point by calling something like this ( from .NET code );

HtmlPage.RegisterCreateableType("PointType", typeof(Point));      

Then you should be able to create one of those from script with something like;

            var plugin = $get("Xaml1");
            plugin.content.services.createObject("PointType");

Note that my plugin ID is "Xaml1" here as generated by Visual Studio.

I struggled to work out the content.services bit for quite a while until I finally found an example in a lab here. The joys of working with script languages :-(

Note, if you've created a scriptable type and registered that with RegisterScriptableType and if that type has scriptable members that use complex types as parameters and so on then you don't have to go through calling RegisterScriptableType as Wilco explains in his excellent post here ( which you should read all of if you're doing any of this stuff ). Your registered scriptable type magically gets a createManagedObject method on it that you can use to achieve the same thing with slightly less code.

Share


Comments

Comments RSS RSS
No comments

Add Comment

 
 

   
  
  
   
Please add 8 and 2 and type the answer here: