Programming with .NET - Generalhttp://forums.silverlight.net//17.aspx/1?Programming+with+NET+GeneralGeneral discussions around authoring Silverlight .NET applications.Mon, 01 Jan 0001 00:00:00 -05001736890http://forums.silverlight.net//p/11550/36890.aspx/1?Call+C+method+from+javascript+in+Silverlight+2+0+Beta1Call C# method from javascript in Silverlight 2.0 Beta1 <p>Hello!<br> &nbsp;</p> <p>I am having some problems with the new version of Silverlight when I try to call a method in my codebehind from javascript. Where exactly do I need to put the RegisterScriptableObject method call?</p> <p>In my&nbsp; App.cs or in the Page.cs? In the OnStartup event, or in one of the constructors?</p> <p>I have tried every possibility I could imagine, but when I try to access the scriptable object from javascript I keep getting the error:</p> <p><span class="objectBox objectBox-errorMessage hasTwisty hasBreakSwitch">&quot;Trying to get unsupported property on scriptable plugin object!&quot;</span> .</p> <p>&nbsp;I tried debugging it with Firebug. The SilverlightControl.Content property seems to be of the type 'Grid', but I can't guess any properties or methods this Content might have...</p> <p>&nbsp;</p> <p>Any suggestions?</p> <p>W.<br> &nbsp;</p> <p>P.S.: The code that used to work with the alpha version isn't working anymore.&nbsp;</p> 2008-03-13T20:34:43-04:0036910http://forums.silverlight.net//p/11550/36910.aspx/1?Re+Call+C+method+from+javascript+in+Silverlight+2+0+Beta1Re: Call C# method from javascript in Silverlight 2.0 Beta1 <p>Add HtmlPage.RegisterScriptableObject(&quot;Bridge&quot;, this);&nbsp; to the Page_Loaded () event.&nbsp; Make sure to add the [ScriptableMember] to the method you want to call from JS.<br> </p> 2008-03-13T21:39:01-04:0036911http://forums.silverlight.net//p/11550/36911.aspx/1?Re+Call+C+method+from+javascript+in+Silverlight+2+0+Beta1Re: Call C# method from javascript in Silverlight 2.0 Beta1 <p>The place to put it would be on your Page class, therefore Page.xaml.cs.</p> <p>So on the Page class you'd put the ScriptableType attribute and then on the method you want visible from JavaScript you'd put the ScriptableMember attribute. Don't forget to call HtmlPage.RegisterScriptableObject() in the constructor of the UserControl derived class. For example:</p> <p>&nbsp;</p> <p><b>[ScriptableType]</b><br> public partial class Page: UserControl<br> {<br> &nbsp; public Page()<br> &nbsp; {<br> &nbsp;&nbsp;&nbsp; InitializeComponent();<br> &nbsp;&nbsp;&nbsp; <b>HtmlPage.RegisterScriptableObject(&quot;Page&quot;, this);</b><br> &nbsp; }<br> <br> &nbsp; <b>[ScriptableMember]</b><br> &nbsp; public void Method1()<br> &nbsp; {<br> &nbsp; }<br> }</p> <p>I hope that helps to clarify it. Things changed a little bit between 1.1 Alpha and 2.0 Beta 1.<br> &nbsp;</p> 2008-03-13T21:39:21-04:0036920http://forums.silverlight.net//p/11550/36920.aspx/1?Re+Call+C+method+from+javascript+in+Silverlight+2+0+Beta1Re: Call C# method from javascript in Silverlight 2.0 Beta1 <p>&nbsp;Just an FYI, you don't need to add <b>[ScriptableType] </b>to the class, only <b>[ScriptableMember]</b> is needed.<br> </p> 2008-03-13T21:56:07-04:0036921http://forums.silverlight.net//p/11550/36921.aspx/1?Re+Call+C+method+from+javascript+in+Silverlight+2+0+Beta1Re: Call C# method from javascript in Silverlight 2.0 Beta1 <p>I have the attributes set, and I did try to register the object in the Page constructor as well... But still no success.</p> <p>How do you call the method from javascript?</p> <p>&nbsp;I use:</p> <p>[SilverlightControl].Content.[ScriptableObjectKey].[Methodname] (whitout the brackets of cource)</p> <p>&nbsp;</p> <p>Is this correct?<br> &nbsp;</p> 2008-03-13T21:57:30-04:0036927http://forums.silverlight.net//p/11550/36927.aspx/1?Re+Call+C+method+from+javascript+in+Silverlight+2+0+Beta1Re: Call C# method from javascript in Silverlight 2.0 Beta1 <p>If you are using CraigN's code, then you should be good to go from a Silverlight perspective.&nbsp; This sounds like a JavaScript problem.&nbsp; </p> <p>Can you post your JavaScript code too?&nbsp;</p> <p>&nbsp;</p> <p>In the meantime:<br> Take a look at my post on how to consume a json object in Silverlight.&nbsp; This is over kill for what you are trying to do, but this shows how to accomplish what you're doing:</p> <p><a href="http://simplesilverlight.wordpress.com/2008/03/12/consume-a-json-object-in-silverlight/" title="http://simplesilverlight.wordpress.com/2008/03/12/consume-a-json-object-in-silverlight/">http://simplesilverlight.wordpress.com/2008/03/12/consume-a-json-object-in-silverlight/</a></p> <p>Also there is a link at the bottom of the post that shows the example.&nbsp; Take a look at the JS there.<br> </p> 2008-03-13T22:09:10-04:0036931http://forums.silverlight.net//p/11550/36931.aspx/1?Re+Call+C+method+from+javascript+in+Silverlight+2+0+Beta1Re: Call C# method from javascript in Silverlight 2.0 Beta1 <p>My Javascript code:&nbsp;</p> <p>var slApp;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; function OnLoaded(sender, args)<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; slApp = sender;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; function TestIt()<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; slApp.Content.SLJSTest.TestMethod();<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p> <p>&nbsp;</p> <p>The html:</p> <p>&lt;div id='errorLocation' style=&quot;font-size: small;color: Gray;&quot;&gt;&lt;/div&gt;<br> &nbsp;&nbsp;&nbsp; &lt;input id=&quot;Button1&quot; type=&quot;button&quot; value=&quot;button&quot; onclick=&quot;TestIt()&quot;/&gt;<br> &nbsp;&nbsp;&nbsp; &lt;div id=&quot;silverlightControlHost&quot;&gt;<br> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;object data=&quot;data:application/x-silverlight,&quot; type=&quot;application/x-silverlight-2-b1&quot; width=&quot;100%&quot; height=&quot;100%&quot;&gt;<br> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;param name=&quot;source&quot; value=&quot;TestApp.xap&quot;/&gt;<br> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;param name=&quot;onerror&quot; value=&quot;onSilverlightError&quot; /&gt;<br> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;param name=&quot;background&quot; value=&quot;white&quot; /&gt;<br> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;param name=&quot;onload&quot; value=&quot;OnLoaded&quot; /&gt;<br> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <br> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;a href=&quot;http://go.microsoft.com/fwlink/?LinkID=108182&quot; style=&quot;text-decoration: none;&quot;&gt;<br> &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;img src=&quot;http://go.microsoft.com/fwlink/?LinkId=108181&quot; alt=&quot;Get Microsoft Silverlight&quot; style=&quot;border-style: none&quot;/&gt;<br> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;/a&gt;<br> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;/object&gt;<br> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;iframe style='visibility:hidden;height:0;width:0;border:0px'&gt;&lt;/iframe&gt;<br> &nbsp;&nbsp;&nbsp; &lt;/div&gt; </p> <p>Edit:</p> <p>Ahhhhhhhh... never mind.. I figured it out... guess the scripteable object is not set yet, when the onloaded event fires... :(</p> <p>Should have figured it out sooner...&nbsp;</p> 2008-03-13T22:29:08-04:0036974http://forums.silverlight.net//p/11550/36974.aspx/1?Re+Call+C+method+from+javascript+in+Silverlight+2+0+Beta1Re: Call C# method from javascript in Silverlight 2.0 Beta1 <p>&nbsp;This is a big problem.&nbsp; The &lt;param name=&quot;onload&quot; value=&quot;OnLoaded&quot; /&gt; gets fired when the plugin has loaded and before the Page_Loaded in Silverlight is executed.&nbsp; <br> </p> 2008-03-14T02:06:13-04:0044851http://forums.silverlight.net//p/11550/44851.aspx/1?Re+Call+C+method+from+javascript+in+Silverlight+2+0+Beta1Re: Call C# method from javascript in Silverlight 2.0 Beta1 <p>&nbsp;hey buddies</p> <p>try this</p> <p>&nbsp;c# code <br> HtmlPage.Window.Invoke(&quot;javascriptfunname&quot;, parameter);&nbsp;</p> <p>javascript code</p> <p>&nbsp;function javascriptfunname(parameter)</p> <p>{</p> <p>}</p> <p>this work properly</p> <p>there is no need to use scriptable<br> &nbsp;</p> 2008-04-08T10:07:50-04:00