Results 1 to 5 of 5

Thread: How to invoke methods of C # in Silverlight using javascript

  1. #1
    Join Date
    Jan 2011
    Posts
    34

    How to invoke methods of C # in Silverlight using javascript

    I have recently installed the Silverlight 2.0 on my windows machine and I want to invoke some C# methods from this application. I know that it would be possible to do so but it would needing some really smart java scripting which I am not aware of much. Are there any developers who can help me to know the proper script that can help me to call C# methods from the Silverlight?

  2. #2
    Join Date
    May 2009
    Posts
    527

    Re: How to invoke methods of C # in Silverlight using javascript

    Silverlight 2.0 applications have a close collaboration with the host browser, especially thanks to the classes in the namespace System.Windows.Browser, which in fact allow for interaction between methods written in managed code within the Silverlight controls and code written in JavaScript on the page that hosts the control. For us web developers in the area. NET, this thing is a true revolution because we have always been accustomed to the notion that our C # or VB.NET code was executed on the server side, while JavaScript was executed solely on the client side, but now, Since Silverlight is a plug-in then run the code on the client side, this concept is totally devastated.

  3. #3
    Join Date
    May 2009
    Posts
    637

    Re: How to invoke methods of C # in Silverlight using javascript

    Side managed code, Silverlight provides HtmlPage class that allows interaction between Silverlight and the page. Interaction contrary, then by Javascript code to managed code, but must be prepared in a manner a little more detail. First, within the Silverlight Loaded event to be recorded by the object which then can be made to the communication from the page.
    This registration is done by calling RegisterScriptableObject, always the class HtmlPage:

    Code:
      private void UserControl_Loaded (object sender, RoutedEventArgs e)
    
      {
    
      HtmlPage.RegisterScriptableObject (SilverlightToJavascriptControlObject ", this);
    
      }
    We note that the key choice is entirely arbitrary. Then instead, we must choose the method that we want to be callable from JavaScript and decorate it with the attribute "ScriptableMember.

    Code:
      [ScriptableMember]
    
      public void AddItem (string value)
    
      {
    
      listbox.Items.Add (value);
    
      HtmlPage.Window.Alert ("called from javascript");
    
      }

  4. #4
    Join Date
    May 2009
    Posts
    511

    Re: How to invoke methods of C # in Silverlight using javascript

    By doing this mentioned in previous post, you just enabled our Silverlight application to receive information from the page that hosts it, in reality, what actually can be called is the method "AddItem". Here you see the Javascript code necessary to make that call:

    Code:
     <Script type = "text / javascript">
    
      SendToSilverlight function ()
    
      {
    
      var txt = document.getElementById ("txt");
    
      var silverlight = document.getElementById ("silverlightControl");
    
      silverlight.Content.SilverlightToJavascriptControlObject.AddItem (txt.value);
    
      }
    
      </ Script>
    
      <Body>
    
      Send something to Silverlight ... <br    />
    
      <Input type = "text" id = "txt" />
    
      <Input type = "button" onclick = "javascript: SendToSilverlight ()" value = "send" />
    
      <Br    /> <Br    />
    
      <Div id = "silverlightControlHost">
    
      <Object id = "silverlightControl" 
    
      data = "data: application/x-silverlight-2" 
    
      type = "application/x-silverlight-2" 
    
      width = "100%" height = "100%">
    
      <Param name = "source" value = "ClientBin / SilverlightAndJavascript.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>
    
      </ Body>

  5. #5
    Join Date
    May 2009
    Posts
    539

    Re: How to invoke methods of C # in Silverlight using javascript

    In above case of using a simple html pages for the use of the Silverlight application, you must first remove the element of type "object" (by its id) that represents the control Siverlight real and, in Then call the AddItem method. Similarly, you can perform the same operation, even within an ASP.NET page:

    Code:
    <Head runat = "server">
    
      <Script language = "javascript" type = "text / javascript">
      SendToSilverlight function () 
      {
      var txt = document.getElementById ("txt");
      var silverlight = document.getElementById ("Xaml1");
      silverlight.Content.SilverlightToJavascriptControlObject.AddItem (txt.value);
      }
      </ Script>
      </ Head>
      <Body style = "height: 100%; margin: 0;">
      <Form id = "form1" runat = "server" style = "height: 100%">
    
      <Asp: ScriptManager ID = "ScriptManager1" runat = "server"> </ asp: ScriptManager>
    
      Send something to Silverlight ... <br    />
    
      <Asp: TextBox ID = "txt" runat = "server" />
    
      <Asp: Button ID = "btn" runat = "server" Text = "Send" OnClientClick = "SendToSilverlight (); return false;" /> <br    /> <Br    />
    
      <Div style = "height: 100%">
    
      <Asp: Silverlight ID = "Xaml1" runat = "server" 
    
      Source = "~ / ClientBin / SilverlightAndJavascript.xap" 
    
      MinimumVersion = "2.0.31005.0" 
    
      Width = "100%" Height = "100%" />
    
      </ Div>
     </ Form>
      </ Body>
    In this case, the identifier to retrieve the control is the same used in the ID attribute of the asp: Silverlight component that is committed to render its element type "object". How could you see it is really easy to interact with the page hosting the Silverlight control and vice versa, through classes offered by the. NET Framework. Clearly then, the information is exchanged and are part of what the user input must pass under the appropriate validation checks to prevent any kind of vulnerability.

Similar Threads

  1. Replies: 3
    Last Post: 17-04-2011, 04:55 PM
  2. SetPassword and ChangePassword Invoke
    By manishdk in forum Active Directory
    Replies: 2
    Last Post: 12-04-2011, 10:22 PM
  3. How to invoke JavaScript Code from an Applet?
    By Soumen in forum Software Development
    Replies: 5
    Last Post: 18-02-2010, 01:02 AM
  4. Invoke servlet from EJB components?
    By Nurhan in forum Software Development
    Replies: 3
    Last Post: 01-04-2009, 09:59 AM
  5. How to invoke servlet filters
    By Pandya in forum Software Development
    Replies: 2
    Last Post: 30-03-2009, 01:31 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,711,689,950.62555 seconds with 17 queries