Skip to main content
Home Forums Silverlight Programming Programming with JavaScript Passing ManagedObject to Javascript method
4 replies. Latest Post by tanlinth on September 1, 2008.
(0)
tanlinth
Member
32 points
40 Posts
08-31-2008 6:10 AM |
Hi,
Please can someone give me an example code for passing in a managed instance to a JavaScript function? or How to do this?
For example:
I have the following class(es):
1 namespace myProject 2 {3 [ScriptableType]4 public partial class Page : UserControl5 {6 SomeClass myObject = SomeClass();7 8 void Page_Loaded(object sender, RoutedEventArgs e) {9 HtmlPage.RegisterScriptableObject("myObject", myObject);10 }11 // methods for my silverlight object 12 }13 14 [ScriptableType]15 public class SomeClass()16 {17 [ScriptableMember]18 public int someMember { get; set;}19 }20 }
Is it possible to pass SomeClass object into JavaScript so I can do the following?
1 function setMember(someClassObject) {2 someClassObject.someMember = 1;3 }
Cheers,
Nilu
Skyrunner
Contributor
2489 points
485 Posts
08-31-2008 6:13 AM |
I'm not sure but take a look here
http://broux.developpez.com/private/SL/faq/?page=Javascript#ParametersComplexJS
I'm sorry it's in French.
08-31-2008 9:55 AM |
Thank you for the help :) but it's not quite what I'm looking for. If you look at the other post, I need to be able to access the object through Javascript. Unfortunately, I can't access anything of Silverlight from JavaScript in a 3rd party web site. Even though I enable HTML access. :(
The only other way it seems possible is to pass in the managed object itself as an argument to the JavaScript method. But I can't seem to de-serialise the object passed from Silverlight on the Javascript end. It generates an error, which says reference to NPObject. :(
Thank you for the help though...
gjhdigital
Participant
1415 points
301 Posts
08-31-2008 2:56 PM |
You could pass a JSON object created in c# to javascript, by setting a javascript variable from a c# public variable on an event like:
var myJSON = "<%= myCsharpJSONvariable %>"; function setMember(myJSON) { myJSON.someMember = 1; }
Here is a JSON sample: http://www.gjhdigital.com/gjhdigital/sl/JSONSilverlight/
09-01-2008 11:27 AM |
Thanks for that! :D JSON worked like a charm. Also I found another tutorial that helped me.