Only on rare cases applications created with Silverlight (or Flash) are static – it’s very common to have information coming from a RSS feed, REST service or any other data source. What is more important, these applications usually needs configuration variables (user name, language, products category, etc.) to be passed to them. But how to do all that?
Configuration syntax
Flash uses flashvars object parameter which is easy to use and passed variables automatically become available within actionscript variables scope. Luckily, Silverlight has almost the same thing.
Flash: name1=value1&name2=value2&name3=value3
Silverlight: name1=value1,name2=value2,name3=value3
To start with, lets create a simple script which will form an arguments string (you can use array of params and then implode with ‘,’, but for the sake of simplicity I just use static string):
$connect = '/users/juozas'; $args = "Connect=" . urlencode($connect) . ",id=1,somethingElse=true";
Inside an object element in HTML add this code (depending on framework you (not)use you may need to change it to work as a template or a view script):
<param name="initParams" value="<?php echo $args; ?>" />
Reading configuration in Silverlight
If you start with a default Silverlight project in Visual Studio, you initially have two classes: App and Page. App is a main class, which (by default) on start up creates a new Page instance and assigns it to a root visual element of itself (App), where Page is your actual Silverlight application (visual part, controls, etc.). Standard start up event in App looks like this:
private void Application_Startup(object sender, StartupEventArgs e) { this.RootVisual = new Page(); }
To use parameters from HTML tag, we need to get initParams from the passed e argument (which has type of StartupEventArgs) and set corresponding properties of some object to their values. You can have global static configuration class, but (also for simplicity) I just pass them to the Page instance:
private void Application_Startup(object sender, StartupEventArgs e) { Page page = new Page(); if (e.InitParams.Keys.Contains("Connect") && !string.IsNullOrEmpty(e.InitParams["Connect"])) { page.connectPath = HttpUtility.UrlDecode(e.InitParams["Connect"]); } this.RootVisual = page; }
Here I’m setting connectPath using Connect from initParams. One last thing – don’t trust user input, if you have integers, use Int32.Parse() and always check if passed values are in range of expected values. It’s really important to remember that SQL injections and other similar attacks are also threat in Silverlight (as Flash also).
Outside information sources
However, not all properties can be passed initially – some times it’s required to refresh information from server during actual runtime (think AJAX). You can easily create asynchronous calls to server from Silverlight too (works almost exactly the same as normal JavaScript AJAX script):
public Page() { InitializeComponent(); LoadButton.Click += new RoutedEventHandler(LoadButton_Click); } void LoadButton_Click(object sender, RoutedEventArgs e) { var wc = new WebClient(); wc.OpenReadCompleted +=new OpenReadCompletedEventHandler(wc_OpenReadCompleted); wc.OpenReadAsync( new Uri( "/script.php?id=1", UriKind.Relative ) ); Text.Text = "Loading..."; } void wc_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e) { if ( e.Error != null ) { // Do something with an error Text.Text = e.Error.ToString(); } else { try { // e.Result has a type of Stream, so we need to read it first // (not just cast to string) StreamReader rdr = new StreamReader(e.Result); Text.Text = rdr.ReadToEnd(); } finally { if ( e.Result != null ) e.Result.Close(); } } }
This code creates onClick event which asynchronously downloads “/script.php?id=1″ and sets results to TextBlock element named “Text”. You can use absolute URLs too, but don’t forget that cross-domain requests protection will try to block it. Look here to read more about how to avoid it. Not even localhost will work if you are testing with VisualStudio (witch creates temporary “ASP.NET development server” with random port (not 80)), so make sure to check it first.
As you can see it’s very easy and simple to create dynamical Silverlight applications. I have done some work with Flash too (probably more than with Silverlight, especially with ActionScript language) and I can confirm that there isn’t much difference. Silverlight is more like Flex though.







