Publishing Silverlight 2 Beta

Overview

Silverlight 2 Beta brings the wonders of Silverlight and removes the annoyance of Javascript. The question remains: How do I get Silverlight deployed on my webserver even though it isn’t out yet? This article will walk through creating a simple “hello world” silverlight application and deploying it on your webserver. This article assumes you have Silverlight 2 installed with the Visual Studio 2008 templates.

Create the Silverlight project

  • Open Visual Studio 2008
  • Click File->New Project
  • Under Visual C# click Silverlight
  • Select Silverlight Application, name it what you want, and click ok
  • When it asks you if you would like to create a web project, click add a new and click ok

Now you should have two projects. Under the library, click and open up your Page.xaml file. You are going to add xaml to the Grid control so that it looks like this:

<UserControl x:Class=“SilverlightDeploy.Page”
    xmlns=“http://schemas.microsoft.com/client/2007”
    xmlns:x=“http://schemas.microsoft.com/winfx/2006/xaml”
    Width=“400" Height="300”>
    <Grid x:Name=“LayoutRoot" Background="White”>
        <Button x:Name=“BtnHelloWorld" Content="Hello World”
                Width=“100" Height="50" Click="BtnHelloWorld_Click” />
    </Grid>
</UserControl>

Then in the Page.xaml.cs file, you need to add the code for the button’s Click event:

public partial class Page : UserControl
{
  public Page()
  {
    InitializeComponent();
  }
  private void BtnHelloWorld_Click(object sender,
    RoutedEventArgs e)
  {
    this.BtnHelloWorld.Content = "You Clicked Me!;
  }
}

Now you can build the application, but it is not quite publish ready.

Publishing the application

First thing we need to do is create a publish version of the application.

Right click on the web application and click "Publish Website” Specify your publish location (I used the desktop), and click ok Now the issue with publishing Silverlight 2 is that webservers don’t recognize the xap file as a proper mime type. The good thing is xap files are essentially just zip files. With this in mind, we can use some wizard trickery to make it all work happily.

  • Go to the folder where you published your web application
  • There should be a .xap file in your ClientBin directory. Change the file extension file to .zip instead of .xap
  • Go to any pages that reference this file (Visual Studio will have created a [ProjectName]TestPage.aspx and .html file.
  • Open the SilverlightDeployTestPage.html and find the object section.
  • Rename the .xap reference to .zip as done in the ClientBin file
<object data=“data:application/x-silverlight,”
type=“application/x-silverlight-2-b1”
width=“100%" height="100%”>
  <param name=“source" value="ClientBin/SilverlightDeploy.zip”/>
  <param name=“onerror" value="onSilverlightError" />
  <param name="background" value="white" />
  <a href="http://go.microsoft.com/fwlink/?LinkID=108182” style=“text-decoration: none;”>
  <img src=“http://go.microsoft.com/fwlink/?LinkId=108181” alt=“Get Microsoft Silverlight” style=“border-style: none”/>
  </a>
</object>

You should be good to go. You can now ftp the site wherever your heart desires and it will work. For now, you have to do this for all of the references to your xap files. I’ll keep you posted (No pun intended).

Happy Coding