Skip to main content

Microsoft Silverlight

Answered Question Passing ManagedObject to Javascript methodRSS Feed

(0)

tanlinth
tanlinth

Member

Member

32 points

40 Posts

Passing ManagedObject to Javascript method

 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 : UserControl
5 {
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 }
 
also would this update myObject on the Silverlight end? and is it possible to do something like this for the following situation : http://silverlight.net/forums/p/23825/85293.aspx#85293  Any help is much appreciated. Smile

Cheers,  

Nilu   

Skyrunner
Skyrunner

Contributor

Contributor

2489 points

485 Posts

Silverlight MVP

Re: Passing ManagedObject to Javascript method

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.

tanlinth
tanlinth

Member

Member

32 points

40 Posts

Re: Re: Passing ManagedObject to Javascript method

 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
gjhdigital

Participant

Participant

1415 points

301 Posts

Answered Question

Re: Passing ManagedObject to Javascript method

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/

 

tanlinth
tanlinth

Member

Member

32 points

40 Posts

Re: Re: Passing ManagedObject to Javascript method

 Thanks for that! :D JSON worked like a charm. Also I found another tutorial that helped me.

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities