views:

1269

answers:

5

I have a SilverLight application using C#, with 2 main functions that I want to make accessible from JavaScript functions. I have done the RegisterScriptableObject() in the class and set-up the [ScriptableMember] for the functions I want access to.

This the SilverLight object:

<div id="silverlightControlHost">
 <object id="silverlightControl" data="data:application/x-silverlight," type="application/x-silverlight-2" width="1024px" height="300px">
  <param name="source" value="DrawingWaveForm.xap"/>
  <param name="onerror" value="onSilverlightError" />
  <param name="background" value="white" />
  <param name="minRuntimeVersion" value="2.0.31005.0" />
  <param name="autoUpgrade" value="true" />
  <a href="http://go.microsoft.com/fwlink/?LinkID=124807" style="text-decoration: none;">
      <img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight" style="border-style: none"/>
  </a>
 </object>
 <iframe style='visibility:hidden;height:0;width:0;border:0px'></iframe>
</div>

and these are my JavaScript functions:

 function Start()
    {
        var control = document.getElementById("silverlightControl");            
        control.Content.Page.Start();
    }

 function Stop()
    {
        var control = document.getElementById("silverlightControl");            
        control.Content.Page.Stop();
    }

Can anyone tell me where I'm going wrong as it does not seem to work

A: 

It's just a thought but do you perhaps need to make the methods public (they don't appear to be in your code)?

Morten Christiansen
I've tried that and it didn't work, actually, it stopped my other functions working as well.
williamtroup
+1  A: 

You need to ensure your C# functions are marked as Scriptable. See http://silverlight.net/learn/learnvideo.aspx?video=65683 for some walk throughs on how to accomplish this.

Tim Heuer
I've done that, added [ScriptableMember] above both functions, but still get the message: Error: uncaught exception: Error setting property on scriptable plugin object! [plugin exception: Object doesn't support this property or method].
williamtroup
+2  A: 

As timheuer said, [Scriptable] on your Silverlight methods.

Call this in your class:

HtmlPage.RegisterScriptableObject("Page", this);

Call the Silverlight methods marked as Scriptable from your javascript like this:

function CenterMap(latitude, longitude)
{
     var silvercontrol = document.getElementById("ctl00_cphMain_slControl");
     if (silvercontrol)
     silvercontrol.Content.Page.CenterOnCoordinates(latitude, longitude);
}

This page shows you this and how to do the reverse, calling javascript methods from Silverlight. It's a really nice model.

TreeUK
Ignore the dirty hardcoded control id there :p a nice ends with works fine. var silverlightControl = $("[id $='slControl']");
TreeUK
A: 

I was just hammering against this problem myself. The first js function works, but everything else throws this error. After tinkering around, I noticed that changing the underlying C# function that was working successfully was having no impact -- the browser was working with a cached version of the Silverlight control. Try clearing your browser's cache.

Peter Leppert
A: 

Would it be the same problem that was answered at this link? Javascript to Silverlight when Silverlight instanciated with object tag

BPerreault