Quick Silverlight Tip: Initialization Parameters - DevCorner

Quick Silverlight Tip: Initialization Parameters

The scenario: you developing Silverlight application, and you need to pass some parameter inside - for example to define which page will be shown, or to pass user credentials, after he already logged-in in previous non-Silverlight part of application.

So, ho you could do it? The answer is pretty simple - "initParams" in Silverlight object construction.

The parameters could be named, and not named.

Named parameters example:

<object data="data:application/x-silverlight," type="application/x-silverlight-2-b2" width="100%" height="100%">
        <param name="source" value="ClientBin/InitParams.xap"/>
        <param name="onerror" value="onSilverlightError" />
        <param name="background" value="white" />
        <param name="initParams" value="UserName=Alex, AuthID=123, eMail=alexg@sela.co.il" />

Non-Name parameters example:

<object data="data:application/x-silverlight," type="application/x-silverlight-2-b2" width="100%" height="100%">
        <param name="source" value="ClientBin/InitParams.xap"/>
        <param name="onerror" value="onSilverlightError" />
        <param name="background" value="white" />
        <param name="initParams" value="Alex,123,alexg@sela.co.il" />

Once those parameters being passed to application it could be used in code-behind. There is two ways to get those parameters: as a part of StartupEventArgs (in Application_Startup event) or get them directly from Silverlight plug-in.

Here is first way to do it:

//In App.Xaml.cs private void Application_Startup(object sender, StartupEventArgs e) { //In my example, if there is InitParams specified call an overloaded constructor if (e.InitParams.Count > 0) this.RootVisual = new Page(e.InitParams); else this.RootVisual = new Page(); } //In Page.Xaml.cs - somewhere in class:
IDictionary<string, string> initParams;



 

public Page(IDictionary<string, string> parameters) { //do whatever need to be done - for example...
initParams = parameters; }

This approach is good when we use named parameters, because unnamed parameters will return InitParams.Count = 0.

To deal with unnamed parameters, we could get them from plugin properties:

public Page()
{
    InitializeComponent();

    string sParameters = System.Windows.Browser.HtmlPage.Plugin.GetProperty("initParams").ToString();

    //If parameters return
    if (sParameters.Length > 0)
    {
        //Process them....
        string[] parameters = sParameters.Split(new char[] { ',' });

        if (null != parameters)
        {
            //In my case I'm making unnamed parameters behave closely to named parameters
            initParams = new Dictionary<string, string>();
            int counter = 0;

            //I'm adding every value with some genrated key to dictionary collection, 
            //which will enable me to use them exactly like named.
            foreach (var item in parameters)
            {
                string key = "KEY_" + counter;
                string val = item;
                initParams.Add(key, val);

                counter++;
            }
        }
    }

We done... Now we can accept parameters and use it.

 

Enjoy,

Alex

Published Sunday, June 29, 2008 12:39 PM by Alex Golesh

Comments

# ALEX TO &raquo; Blog Archive &raquo; Quick Silverlight Tip: Initialization Parameters

Pingback from  ALEX TO  &raquo; Blog Archive   &raquo; Quick Silverlight Tip: Initialization Parameters

# Quick Silverlight Tip: Communicating between two Silverlight objects on a single page

Today I&#39;ll show how two Silverlight objects (Silverlight applications) could communicate while been

Thursday, July 10, 2008 11:22 AM by DevCorner

# Silverlight Cream for July 16, 2008 -- #326

Corrina Barber updated Red &amp; Black skins, Mike Snow on Isolated Storage, Tim Heuer on S3 and SL2B2

Thursday, July 17, 2008 8:44 AM by Community Blogs

# Silverlight Cream for July 16, 2008 -- #326

Corrina Barber updated Red &amp; Black skins, Mike Snow on Isolated Storage, Tim Heuer on S3 and SL2B2

Thursday, July 17, 2008 12:38 PM by Community Blogs

Leave a Comment

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

Enter the numbers above: