Dynamically setting initialization parameters in Silverlight application

One of the properties on object tag for Silverlight control in initParams or initialization parameters:

 

 <object name="objSilverlight" data="data:application/x-silverlight-2," type="application/x-silverlight-2"

            width="100%" height="100%">

            <param name="source" value="ClientBin/SilverlightInitParams.xap" />

            <param name="onError" value="onSilverlightError" />

            <param name="background" value="white" />

            <param name="minRuntimeVersion" value="3.0.40624.0" />

            <param name="autoUpgrade" value="true" />

            <param name="initParams" value="key=value,key1=value1"/>

            <a href="http://go.microsoft.com/fwlink/?LinkID=149156&v=3.0.40624.0" style="text-decoration: none">

                <img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight"

                    style="border-style: none" />

            </a>

        </object>

We can use this parameter to inform our Silverlight application about a specific piece of data it may need.  To access this data inside Silverlight application, you would need to use arguments from application startup event:

private void Application_Startup(object sender, StartupEventArgs e)

        {

            MainPage page = new MainPage();

            var paramList = e.InitParams.ToList();

Our initial parameters collection is exposed as a dictionary off Startup Event Arguments.

The next step it to dynamically set the parameter.  The problem is that the object tag / Silverlight application creation occurs on the client side, which has no access to server data.  We are going to play a trick here and modify our aspx page to delegate creation of actual parameters string to the page itself:

<param name="initParams" value="<%=GetInitParameter() %>" />

The next step is to write a method in our page that returns a string in format key=value.  Here is entire code behind for our aspx page:

    public partial class _Default : System.Web.UI.Page

    {

        private string _params = string.Empty;

 

        protected void Page_Load(object sender, EventArgs e)

        {

            // put a fake variable into session.  Ordinarily, we are just getting

            // data from somewhere on the server. 

            // could even be a database call if necessary

            Session["User"] = "Sergey Barskiy";

 

            //now set a variable

            _params = string.Concat("User=", Session["User"].ToString());

        }

 

        public string GetInitParameter()

        {

            return _params;

        }

    }

Here is what we do inside our Startup routine in Silverlight application:

 

        private void Application_Startup(object sender, StartupEventArgs e)

        {

            MainPage page = new MainPage();

            var paramList = e.InitParams.ToList();

            page.Key.Text = String.Format("{0} = ", paramList.First().Key);

            page.Value.Text = paramList.First().Value;

 

            RootVisual = page;

 

        }

You can download this sample application here.

9 Comments

  1. Hi,
    I have a small issue regarding loading SL application.
    Can we call application_startup event again (from page load event of aspx page) after navigating to aspx page from silverlight application. I am trying to load the silverlight application in separate browser with some new controls by navigating to the same aspx page which hosts the silverlight application. But its only opening new page and i am not able to display any new control in silverlight application in new browser window.
    Plz can anybody help?

  2. Great.

    For SL 5 and ASP.NET (Control Silverlight), not valid

    <asp:Silverlight InitParameters="Indicador=VentasEvolucion,baseHttpAddress=” ID=”Ctl_GraficaSilverlight” runat=”server”

Leave a Reply

Your email address will not be published. Required fields are marked *