Powered by MSDN

US - English
NEW! Silverlight 5 is available Learn More

Call C# method from javascript in Silverlight 2.0 Beta1 RSS

8 replies

Last post Apr 08, 2008 11:07 AM by naveenkushwaha

(0)
  • watchman

    watchman

    Member

    36 Points

    19 Posts

    Call C# method from javascript in Silverlight 2.0 Beta1

    Mar 13, 2008 09:34 PM | LINK

    Hello!
     

    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?

    In my  App.cs or in the Page.cs? In the OnStartup event, or in one of the constructors?

    I have tried every possibility I could imagine, but when I try to access the scriptable object from javascript I keep getting the error:

    "Trying to get unsupported property on scriptable plugin object!" .

     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...

     

    Any suggestions?

    W.
     

    P.S.: The code that used to work with the alpha version isn't working anymore. 

    c# .net javascript Javascript silverlight c# codebehind "Silverlight 2"

  • cschuman

    cschuman

    Member

    213 Points

    55 Posts

    Re: Call C# method from javascript in Silverlight 2.0 Beta1

    Mar 13, 2008 10:39 PM | LINK

    Add HtmlPage.RegisterScriptableObject("Bridge", this);  to the Page_Loaded () event.  Make sure to add the [ScriptableMember] to the method you want to call from JS.

  • CraigN

    CraigN

    Member

    352 Points

    89 Posts

    Re: Call C# method from javascript in Silverlight 2.0 Beta1

    Mar 13, 2008 10:39 PM | LINK

    The place to put it would be on your Page class, therefore Page.xaml.cs.

    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:

     

    [ScriptableType]
    public partial class Page: UserControl
    {
      public Page()
      {
        InitializeComponent();
        HtmlPage.RegisterScriptableObject("Page", this);
      }

      [ScriptableMember]
      public void Method1()
      {
      }
    }

    I hope that helps to clarify it. Things changed a little bit between 1.1 Alpha and 2.0 Beta 1.
     

    Microsoft Xbox MVP
  • cschuman

    cschuman

    Member

    213 Points

    55 Posts

    Re: Call C# method from javascript in Silverlight 2.0 Beta1

    Mar 13, 2008 10:56 PM | LINK

     Just an FYI, you don't need to add [ScriptableType] to the class, only [ScriptableMember] is needed.

  • watchman

    watchman

    Member

    36 Points

    19 Posts

    Re: Call C# method from javascript in Silverlight 2.0 Beta1

    Mar 13, 2008 10:57 PM | LINK

    I have the attributes set, and I did try to register the object in the Page constructor as well... But still no success.

    How do you call the method from javascript?

     I use:

    [SilverlightControl].Content.[ScriptableObjectKey].[Methodname] (whitout the brackets of cource)

     

    Is this correct?
     

  • cschuman

    cschuman

    Member

    213 Points

    55 Posts

    Re: Call C# method from javascript in Silverlight 2.0 Beta1

    Mar 13, 2008 11:09 PM | LINK

    If you are using CraigN's code, then you should be good to go from a Silverlight perspective.  This sounds like a JavaScript problem. 

    Can you post your JavaScript code too? 

     

    In the meantime:
    Take a look at my post on how to consume a json object in Silverlight.  This is over kill for what you are trying to do, but this shows how to accomplish what you're doing:

    http://simplesilverlight.wordpress.com/2008/03/12/consume-a-json-object-in-silverlight/

    Also there is a link at the bottom of the post that shows the example.  Take a look at the JS there.

  • watchman

    watchman

    Member

    36 Points

    19 Posts

    Re: Call C# method from javascript in Silverlight 2.0 Beta1

    Mar 13, 2008 11:29 PM | LINK

    My Javascript code: 

    var slApp;
            function OnLoaded(sender, args)
            {
                slApp = sender;
            }
            function TestIt()
            {
                slApp.Content.SLJSTest.TestMethod();
            }

     

    The html:

    <div id='errorLocation' style="font-size: small;color: Gray;"></div>
        <input id="Button1" type="button" value="button" onclick="TestIt()"/>
        <div id="silverlightControlHost">
            <object data="data:application/x-silverlight," type="application/x-silverlight-2-b1" width="100%" height="100%">
                <param name="source" value="TestApp.xap"/>
                <param name="onerror" value="onSilverlightError" />
                <param name="background" value="white" />
                <param name="onload" value="OnLoaded" />
               
                <a href="http://go.microsoft.com/fwlink/?LinkID=108182" 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>

    Edit:

    Ahhhhhhhh... never mind.. I figured it out... guess the scripteable object is not set yet, when the onloaded event fires... :(

    Should have figured it out sooner... 

  • cschuman

    cschuman

    Member

    213 Points

    55 Posts

    Re: Call C# method from javascript in Silverlight 2.0 Beta1

    Mar 14, 2008 03:06 AM | LINK

     This is a big problem.  The <param name="onload" value="OnLoaded" /> gets fired when the plugin has loaded and before the Page_Loaded in Silverlight is executed. 

  • naveenkushwaha

    naveenkushwaha

    Member

    52 Points

    28 Posts

    Re: Call C# method from javascript in Silverlight 2.0 Beta1

    Apr 08, 2008 11:07 AM | LINK

     hey buddies

    try this

     c# code
    HtmlPage.Window.Invoke("javascriptfunname", parameter); 

    javascript code

     function javascriptfunname(parameter)

    {

    }

    this work properly

    there is no need to use scriptable