locked
Silverlight 5 - SilverightControl.Content.services is null or undefined when called from Javascript RRS feed

  • Question

  • Hi guys,

    For some reasons, I can not create managed type from Javascript, it said "services" is undefined. I can obtain the Content of Silveright plugin control without any problems. Is there reason why the services is undefined?

    1. Codes in Silverlight

      
     Dim silverlightModuleLauncher As New SilverlightModuleLauncher()
                HtmlPage.RegisterCreateableType("Location", GetType(Location))
                HtmlPage.RegisterCreateableType("locationInfoParameters", GetType(List(Of LocationInfo)))
                HtmlPage.RegisterScriptableObject("Silverlight", silverlightModuleLauncher)

        2. Javascript

     var silverlightApplication: any = document.getElementById("silverlightObject");
    
    var location = silverlightApplication.Content.services.createObject("Location");

    cheers,

    Khoa

    Thursday, August 20, 2015 11:18 AM

All replies

  • Hi Knoa Ng,

    Please modify your javascript as below code. Before we createObject in javascript, please get the control's host firstly. The Content.services is related with the host.

    <script type="text/javascript">
            var control = null;
            function pluginLoaded(sender, args) {
                control = document.getElementById("silverlightControlHost");
                control = sender.getHost();
            }
    
            function getRandom1To6()
            {
                var random = control.content.services.createObject("RandomNumbers");
                alert("Your number is:" + random.GetRandomNumberInRange(1, 6));
            }
        </script>
    

    <div id="silverlightControlHost">
            <object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="100%" height="100%">
        <param name="source" value="ClientBin/SilverlightApplication1.xap"/>
        <param name="onError" value="onSilverlightError" />
        <param name="background" value="white" />
        <param name="minRuntimeVersion" value="5.0.61118.0" />
        <param name="autoUpgrade" value="true" />
        <param name="onLoad" value="pluginLoaded" />
    
        <a href="http://go.microsoft.com/fwlink/?LinkID=149156&v=5.0.61118.0" style="text-decoration:none">
          <img src="http://go.microsoft.com/fwlink/?LinkId=161376" alt="Get Microsoft Silverlight" style="border-style:none"/>
        </a>
         </object><iframe id="_sl_historyFrame" style="visibility:hidden;height:0px;width:0px;border:0px"></iframe></div>
    


    Best Regards,
    Weiwei

    Friday, August 21, 2015 3:40 AM
    Moderator
  • Hi Weiwei,

    Object does not support property or method 'getHost' when sender.getHost() is called. Do you know why?

    With my existing codes from the first message, I am able to call Scriptable method in Silverlight so I think there is nothing wrong with the getting the Silverlight plugin object but I might be wrong. I just don't get it why services property is Null


    • Edited by Khoa Ng Friday, August 21, 2015 4:12 AM
    Friday, August 21, 2015 4:10 AM
  • Hi Khoa Ng,

    I get the error of services is undefined or null when I don't call the sender.getHost(). But when I add the getHost(), it works fine in my side. Following code is the ScriptMember in my Silverlight client. The code in the aspx page have been posted in my first reply. Please compare all of my code with yours, whether there has any detailed difference.

        public partial class MainPage : UserControl
        {
            public MainPage()
            {
                InitializeComponent();
    
                HtmlPage.RegisterCreateableType("RandomNumbers", typeof(RandomNumbers));
            }
        }
    
        public class RandomNumbers
        {
            private Random random = new Random();
    
            [ScriptableMember()]
            public int GetRandomNumberInRange(int from, int to)
            {
                return random.Next(from, to + 1);
            }
        }

    Best Regards,
    Weiwei

    Friday, August 21, 2015 5:44 AM
    Moderator