Jun 20

Getting your initParams from managed code

Posted in Silverlight      Comments Off on Getting your initParams from managed code

You can use the initParams property of the Siverlight control to pass aditional infomation during the creation of the control. There”s already a property which should let you get it from managed code:StartupArguments. Unfortunately, it wil always return an empty string in the current release:


Yasser suggested a workaround for the current version untill we get a new release that lets us get the value correctly. His idea was to call a javascript method that will return that value (you need this because the InitParams is a property of the Silverlight control and in the current release there isn”t a way to get a reference to that control from managed code). Doing this means that you”ll have to:

  • build a new EventArgs class and annotate it with the Scriptable attribute
  • build a new event based on a new delegate and annotate it with the Scriptable attribute
  • annotate the class that exposes the event with the Scriptable attribute
  • register that class by calling the RegisterScriptableObject method

Lots of things for getting a string from JS code (btw, i”ve already talked about this sort of code here). So, why not simplify? I suggest just adding a string property to the class which will get called from the page. You get a little more coupling with this approach, but you reduce the code (and when you think about it, you have coupling in  both cases since things won”t work if you”re exposing an event and it doesn”t get handled by the page!). Here”s some managed demo code:

public partial class Page2 : Canvas
  public void Page_Loaded(object o, EventArgs e)
     // Required to initialize variables
     WebApplication.Current.RegisterScriptableObject(“SL”, this);

  private string _initParams; 
  public string InitParams
     get{return _initParams;}
     set { _initParams = value; info.Text = _initParams; }


As you can guess by looking, the top canvas has a TextBlock inside that is used to show the values passed to the InitParams property. To test the page, lets o write the following JS code:

function createSilverlight()
           source: “Page2.xaml”,
           parentElement: document.getElementById(“SilverlightControlHost”),
           id: “SilverlightControl”,
           properties: {
               width: “100%”,
               height: “100%”,
               version: “0.95”,
               background: “#ffffffff”,
               isWindowless: false,
               enableHtmlAccess: true
          events: {onLoad:handleLoad},

And from the handleLoad method we initialize the InitParams property:

function handleLoad(sender){
   sender.content.SL.InitParams = sender.InitParams;

And that”s it! Oh, there”s a small gotcha: the InitParams isn”t initialized during the load event of the top canvas object. So, if you have aditional logic that depends on that value, don”t put it on a method that handles that event. In those cases, you”ll have to tweak your code so that it”ll only run after getting a valid value for the property.