Configuring Silverlight Applications

Gautham Janardhan
With most software applications trying to cater to multiple user needs nowadays, the focus is to make them as configurable as possible. For e.g., when creating Silverlight applications which will connect to WCF services, the service end point usually needs to be configurable. Putting this information within the code makes it rather rigid as changing this at the time of implementation would mean recompiling your entire assembly. Due to this, most applications tend to use configuration settings for such dynamic parameters.

One of the ways of passing configuration values to the Silverlight application is through “initParams” but this can become quite tricky especially when the number of settings is more or if your settings are in the form of layered objects. Imagine if you need to make around 5 parameters configurable (Name,Age,Sex,Country and Location),your initParams would look something like this: “Name=Gautham,Age=26,Sex=Male,Country=India,Location=Bangalore”. Slightly clumsy. Now, imagine if each of these is a complex layered object. Very clumsy, right?

An alternative I find very useful is to use a xaml file to do this. The concept is similar to having a web configuration file for your application, wherein your configuration file resides in your root web site. I am going to explain this with the help of a small example using c# code, visual studio 2008 as IDE and Silverlight 3.0.

To begin with, let’s create a normal silver light application and a web project to host it.

Our next step would be to create a data class which would hold the values that we need to pass to the Silverlight application. A sample is shown below

namespace SilverlightConfigurationThroughXamlFile 
    public class Config 
        public Config() 
            SomeChildren = new List<ConfigChildren>(); 
        public List<ConfigChildren> SomeChildren { get; set; } 
        public string MainSetting1 { get; set; } 
        public string MainSetting2 { get; set; } 
        public string MainSetting3 { get; set; } 
        public string MainSetting4 { get; set; } 
    public class ConfigChildren 
        public string Setting1 { get; set; } 
        public bool Setting2 { get; set; } 
        public int Setting3 { get; set; } 

Open in new window

This class can be modified as per your requirements. Now the task is to add a configuration file to the project hosting the Silverlight application. We will add a new file to the host project and name it Config.xaml which will contain the code shown below.

<local:Config xmlns:local="clr-namespace:SilverlightConfigurationThroughXamlFile;assembly=SilverlightConfigurationThroughXamlFile"
              xmlns:x="" xmlns:sys='clr-namespace:System;assembly=mscorlib'
              MainSetting1='test 1234' MainSetting2='ABCD 1234' MainSetting3='EFGH 1234' MainSetting4='IJKL 1234'>
  <local:Config.SomeChildren >
      Setting1='Hello world' Setting2='true' Setting3='26' />

Open in new window

Now the task will be to read this file and store this into our data class. For this we utilize the XamlLoader.Parse method. We will be doing this in the app startup method. So, go to the method “Application_Startup” in app.xaml file  and modify it as shown below:
private void Application_Startup(object sender, StartupEventArgs e)
            this.RootVisual = new MainPage();
            WebClient client = new WebClient();
            client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
            client.DownloadStringAsync(new Uri("/Config.xaml", UriKind.Relative));

Open in new window

The following supporting method also needs to be added to the same file:
private void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
            Config config = (Config)XamlReader.Load(e.Result);
            // this config object can later stored into an application level variable

Open in new window

Compiling and running the application, you can see that the config object will now contain all the values that we set in the config.xaml.This can now be stored into an application level variable and used in code.
Gautham Janardhan

Comments (0)

Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.

Get access with a 7-day free trial.
You Belong in the World's Smartest IT Community