An Exception is thrown when calling a Silverlight method from JavaScript.

One question I keep seeing all the time in the forums, sorry the social platforms is when people call methods registered in Silverlight to get the ASP.NET interaction that everybody wants and loves, yet, when checking if the method got registered in Silverlight as an ScriptableMember instead of being able to check for null, the conditional statement throws an exception. We talked about JavaScript and Silverlight communication on my previous post.

Silverlight is the last object in the page to be render, therefore the last one to initialize, for this, many tools have been provided as an object in the page, like InitParams to pass initial information, yet people want to signal when a constructor got called or in a Page application when you navigated to a page so you can let the ASP.NET page about it.

Just because the property of the object is not there yet, so you cannot do this:

   1: var silverlightControl = document.getElementById('Xaml1');
   2:  
   3: if ( silverlightControl.Content.SilverlightApp == null )
   4:  .....

Bad code! will throw an exception, is not really bad code in Javascript, yet think that Content and SilverlightApp is not created yet, so you should check for Content to be null first?

There are a few way to handle that in JavaScript to find out if the

In Silverlight we register the class and method so JavaScript can see if the application got loaded, for once, you can use initParams to pass information after loaded. In the example below we call JavaScript on the constructor of a UserControl, yet you may want to do that onNaviagated even of the page, a little more useful and you can keep in sync the ASP.NET page, without full postbacks, with the Silverlight application.

   1: [ScriptableType]
   2:  public partial class MainPage : UserControl
   3:  {
   4:      public MainPage()
   5:      {      
   6:          InitializeComponent();
   7:  
   8:          HtmlPage.RegisterScriptableObject("SilverlightApp", this);
   9:  
  10:          HtmlPage.Window.Invoke("AlSL", "");
  11:          }
  12:       
  13:      }
  14:  
  15:      [ScriptableMember]
  16:      public void Navigate(string sWhere)
  17:      {
  18:          ContentFrame.Source = new Uri(sWhere, UriKind.Relative);
  19:      }

In JavaScript we create a method than once call, calls back to Silverlight:

   1: function AlSL() {
   2:            
   3:             
   4:             var silverlightControl = document.getElementById('Xaml1');
   5:                         
   6:             silverlightControl.Content.SilverlightApp.Navigate("/LetsGo");
   7:             
   8:         }

Hope this little sample helps the user scenarios describe above, yet making things simple and just calling Silverlight once at the object creation using InitParams will be the recommended way.

Cheers

Al



Published Thursday, June 03, 2010 8:05 PM by albertpascual
Filed under: ,

Comments

No Comments

Leave a Comment

(required) 
(required) 
(optional)
(required)