Silverlight 2.0 Beta 1 shipped yesterday, so I thought now would be a good time to deploy a new version of SilverLife. SilverLife is a Silverlight implementation of John Conway’s game of Life. Version 1.0, which I published in August of last year, targeted the Silverlight 1.1 alpha. Version 2.0 targets the new Silverlight 2.0 beta and can be viewed at https://training.atmosera.com/silverlife. You can download the source code from training.atmosera.com/downloads/silverlife_2.0.zip.
SilverLife 2.0 looks almost exactly like version 1.0, save for the buttons at the bottom of the grid. Silverlight 1.1 had no controls built in, so I had to manufacture something that looked like buttons. Silverlight 2.0 Beta 1 contains more than 20 built-in control types, so I used Button controls. I also took advantage of the new style support in Silverlight 2.0 to stylize the buttons. First I defined a style named “LifeButton” in XAML:
<Style x_Key=”LifeButton” TargetType=”Button”>
<Setter Property=”Width” Value=”132″ />
<Setter Property=”Height” Value=”44″ />
<Setter Property=”FontSize” Value=”16″ />
</Style>
Then I applied the style to the four Button controls:
<Button x_Name=”StartButton” Style=”{StaticResource LifeButton}” … />
<Button x_Name=”StopButton” Style=”{StaticResource LifeButton}” … />
<Button x_Name=”StepButton” Style=”{StaticResource LifeButton}” … />
<Button x_Name=”ClearButton” Style=”{StaticResource LifeButton}” … />
This is an extremely simple example, but styles can be much more complex and can even use templates to completely redefine a control’s visual tree. Styles and templates are going to be incredibly important to Silverlight 2.0 apps, so I’ll be writing more about them later.
Porting SilverLife from Silverlight 1.1 to 2.0 required just a handful of changes. First I used Visual Studio 2008 to create a new Silverlight project. Then I imported most of the code and XAML from the first version of SilverLife and began patching things up. Here are some of the changes that were required:
- The XamlReader class moved to the System.Windows.Markup namespace
- XamlReader.Load now requires an http://schemas.microsoft.com/client/2007″>http://schemas.microsoft.com/client/2007” attribute
- HtmlPage.Document.GetElementByID changed to HtmlPage.Document.GetElementById
- The IsolatedStorageFileStream constructor now throws an IsolatedStorageException rather than an IOException if you attempt to open a file that doesn’t exist
One of the most pleasant surprises in Silverlight 2.0 Beta 1 is the simplified deployment strategy. XAML files and other resources (for example, images) are now compiled into the application’s assembly. Moreover, the assembly and any assemblies it depends upon are packaged in a XAP file, which is a glorified zip file containing a manifest. The upshot is that deploying the application to Wintellect’s Web server required two simple steps:
- Copy Default.html (the page that hosts the application) into the Silverlife directory
- Create a ClientBin folder in that directory and copy Silverlife.xap into it
That’s it! Other than adding a new MIME type (“application/x-silverlight-app”) to IIS and mapping it to XAP files (a one-time procedure), deployment involved nothing more copying a couple of files. How cool is that?
Another pleasant surprise was the fact that Silverlight 2.0 displays a default progress UI as the XAP file is downloaded. Now users won’t have to stare at a blank canvas as they wait for assemblies to be downloaded. Nor will you, the developer, have to code up a progress UI.
I’ll have much, much more to say in future posts. A new day is dawning, and it has Silverlight 2.0 written all over it!