none
How to access Silverlight objects from javaScript function ?

    Question

  • Hello,

    is that possible to access Silverlight controls from javaScript ? It will be better if it also allows jQuery .

    Is that possible ? If yes can you please tell how to do that ?

    Friday, March 16, 2012 10:11 PM

Answers

  • Yes, it's possible.

    Add the following to your *.aspx file:

    <script type="text/javascript">
            function JStoSL(message) {
                var control = document.getElementById('silverlightControl');
                var rslt = control.Content.App.JStoSL(message); // in your project there should be HtmlPage.RegisterScriptableObject("App", ...) call
                //alert(rslt);
                return rslt;
            }
        </script>

    and add id for Silverlight like this:

    <object id="silverlightControl" data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="100%" height="100%">

    then for example in MainPage.xaml.cs add:

    [ScriptableType]
    public partial class MainPage : UserControl 
    {
        private void Main_Page_Loaded(object sender, RoutedEventArgs e)
        {
          HtmlPage.RegisterScriptableObject("App", this);
        }
    
        [ScriptableMember]
        public string JStoSL(string message)
        {
          HtmlPage.Window.Alert("JavaScript said: " + message);
          return "Silverlight JStoSL done for input: " + message;
        }
        
        private void SendToBtn_Click(object sender, RoutedEventArgs e)
        {
          ScriptObject so = (ScriptObject)HtmlPage.Window.GetProperty("external");
          try {
            var rslt = so.Invoke("Execute", SendToEdit.Text);
            MessageBox.Show("Answer is: " + rslt);
          }
          catch (Exception ex) {
            ErrorWindow.CreateNew(ex);
          }
        }
    }



         

    Friday, March 16, 2012 10:33 PM

All replies

  • Yes, it is possible - it is bi-directional. Pro Silverlight book from APress shows you how to call Silverlight from Javascript...

    As an example, I have a Silverlight App that needs to send the buyer off to PayPal. I have the PayPal button code sitting in HTML on the form; Silverlight positions the button over my Silverlight App; when the user hits the button, a javascript catches the click and passes it pack to the Silverlight app for validation & custom field setting and button code updating before being allowed to post to PayPal...

     

    G.

    Friday, March 16, 2012 10:22 PM
  • Yes, it's possible.

    Add the following to your *.aspx file:

    <script type="text/javascript">
            function JStoSL(message) {
                var control = document.getElementById('silverlightControl');
                var rslt = control.Content.App.JStoSL(message); // in your project there should be HtmlPage.RegisterScriptableObject("App", ...) call
                //alert(rslt);
                return rslt;
            }
        </script>

    and add id for Silverlight like this:

    <object id="silverlightControl" data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="100%" height="100%">

    then for example in MainPage.xaml.cs add:

    [ScriptableType]
    public partial class MainPage : UserControl 
    {
        private void Main_Page_Loaded(object sender, RoutedEventArgs e)
        {
          HtmlPage.RegisterScriptableObject("App", this);
        }
    
        [ScriptableMember]
        public string JStoSL(string message)
        {
          HtmlPage.Window.Alert("JavaScript said: " + message);
          return "Silverlight JStoSL done for input: " + message;
        }
        
        private void SendToBtn_Click(object sender, RoutedEventArgs e)
        {
          ScriptObject so = (ScriptObject)HtmlPage.Window.GetProperty("external");
          try {
            var rslt = so.Invoke("Execute", SendToEdit.Text);
            MessageBox.Show("Answer is: " + rslt);
          }
          catch (Exception ex) {
            ErrorWindow.CreateNew(ex);
          }
        }
    }



         

    Friday, March 16, 2012 10:33 PM
  • the  following  post  will be  very  helpful 

    i  have  explained  briefly  about   how  to  access  silverlight  functions  from  javascript

    http://dotnetstadium.blogspot.in/2012/06/how-to-access-silverlight-function.html

    Wednesday, June 13, 2012 4:35 AM