Silverlight InitParams

14. July 2009
While developing xLite Player 2.0 I needed a way to store a variable in a config file in Silverlight for the url to the XML file it needs to read it's data.  I didn't create a WCF service for this because the XML file only got to about 20KB with about 1000 records in it...so not large at all.  If it was a WCF service this problem would have been really easy to solve...create an entry in appSettings in the web.config and set the key/value pair up and be done

I started exploring ways to do this in Silverlight and here were my options I came up with:

1) create a config.xml file in my Silverlight project that looks something like the following:

<config>
   <FileUrl>http://localhost/websitename/directory/data.xml</FileUrl>
</config>

2) create a public static constant that is in a class
3) store the value in the website web.config
    won't work because my silverlight app ends up being in /ClientBin so it doesn't have
    access to anything except within that folder, plus Silverlight is on the client so no access what so ever to the site.
4) when initializing the Silverlight application use the InitParams attribute and set it there


I chose option number 4. I felt it provides the greatest flexibility and makes it easier to implement the silverlight app for others.  This also keeps it separate from the silverlight project itself (hence why it's easier deployment).

So the code will looks something like the following:

<asp:Silverlight ID="Xaml1" runat="server" Source="~/ClientBin/YourApp.xap" Width="100%" Height="100%" InitParameters="FileUrl=http://localhost/websitename/directory/data.xml"/>  

Then in the App.xaml.cs you need to add code similiar to this:

MediaData is a class I use and I made FileUrl a public static property

private void Application_Startup(object sender, StartupEventArgs e)
{
  MediaData.FileUrl = e.InitParams["FileUrl"];
  //rest of code below
}

And that is that. A good way to store some config settings for your silverlight app. Any other suggestions let me know.

Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkList

Silverlight

blog comments powered by Disqus