Silverlight 3.0 from scratch

Silverlight 3.0 from scratch

For many reasons, and in particular for pedagogical ones or for dynamic code generation, it may be useful to compile Silverlight 3 code on the command-line, without using Visual Studio. It's a good way to discover the Silverlight framework organization. Tools can be found in the Silverlight SDK and in the .NET 3.5 framework.

We will work on a little animation demo application. The source code is available here . The code is written with a simple text editor. It's all in C#, and XAML is not used (however it could be used). It is distributed among 2 files :

  • AnimationDemo.cs, which contains the main UserControl (code not detailed here),
  • SlApplication.cs, which contains the application's entry point.

The loading of the UserControl occurs in the Application Startup event :

public class SlApplication : Application
{
  public SlApplication()
  {
    this.Startup += new StartupEventHandler(slApplication_Startup);
  }
  void slApplication_Startup(object sender, StartupEventArgs e)
  {
    AnimationDemo root = new AnimationDemo();
    this.RootVisual = root;
  }
}

Manual compilation

The standard .NET 3.5 C# compiler is used to compile manually this source code. A search path can be configured through a .bat file containing the following commands :

@Set NetPath=C:\WINDOWS\Microsoft.NET\Framework\v3.5
@Set Path=%NetPath%;%PATH%

A response file (SlApp.rsp) is written to gather the compiler parameters :

/nostdlib+ 
/lib:"C:\Program Files\Microsoft Silverlight\3.0.40818.0"
/r:mscorlib.dll 
/r:System.Core.dll 
/r:system.dll 
/r:System.Windows.dll 
/target:library 
/out:Bin\XapFileCache\SlApp.dll 
..\..\SlApplication.cs
..\..\AnimationDemo.cs

The /nostdlib+ option (equivalent to /nostdlib) avoids to reference the standard .NET framework mscorlib.dll. The Silverlight framework assemblies references are explicitely declared using the /lib (path) and /r options. The option /target:library indicates that compiling produces a class library, whom file is specified through the /out option. The end of the response file shows the source files list.

The following command launches the compilation :

csc @SlApp.rsp

A Silverlight application is deployed as a ZIP format archive file with a .xap extension, containing :

  • the assemblies created by compilation,
  • the referenced assemblies (unless they exist in the Silverlight runtime),
  • a manifest file, AppManifest.xaml, which describes the archive content and the application entry point.

In our case, the manifest file is written manually with the following content :

<Deployment 
  xmlns="http://schemas.microsoft.com/client/2007/deployment"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
  EntryPointAssembly="SlApp" 
  EntryPointType="SlApp.SlApplication" 
  RuntimeVersion="2.0.31005.0">
  <Deployment.Parts>
    <AssemblyPart x:Name="SlApp" Source="SlApp.dll" />
  </Deployment.Parts>
</Deployment>

The manifest file and the assembly are then gathered in a .zip file, manually or through a command-line tool. The .zip extension is renamed as .xap.

If you use the File Explorer of Windows Seven to create the .xap file, you must modify its security properties to give the Read and Execution rights to the Users group.

To test our application, we write an HTML test page, in which the .xap archive is referenced by an <object> tag :

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<!-- saved from url=(0014)about:internet -->
<head>
    <title>SlApplication</title>
    <style type="text/css">
    html, body 
    {
        height: 100%;
        overflow: auto;
    }
    </style>
</head>
<body>
    <object data="data:application/x-silverlight,
	"type="application/x-silverlight-2"
	width="100%" height="100%">
	<param name="source" value="SlApp.xap"/>
	<param name="minRuntimeVersion" value="3.0.40818.0" />
    </object>
</body>
</html>

We can now test our application :

Silverlight application

Note that to run the application from a web site, the application/x-silverlight-2 MIME type has to be associated with the .xap extension in the server parameters.

Application composed of multiple assemblies

Generally, a Silverlight application is composed of multiple assemblies, which can be built and then gathered in the .xap archive using the same principle as described over. The version 2.0 of the demo application shows this kind of organization.

Some Silverlight components don't exist in the standard runtime, but are in assemblys that have to be explicitely referenced and deployed in the .xap archive. Thus, the DatePicker control requires to deploy System.Windows.Controls.dll, which is not in the runtime. On the developer workstation, these complementary assemblies are installed in C:\Program Files\Microsoft SDKs\Silverlight\v3.0\Libraries\Client. The version 3.0 of the demo application uses this kind of deployment.

MSBuild compilation

The process that we have described over may be simplified and automatized using MSBuild (a tool installed with the SDK).

In particular, MSBuild has the advantage to automatically generate the .xap file after compilation. Further, when assemblies like System.Windows.Controls.dll should be deployed, MSBuild manages the resources localization and deployment.

We'll not get here into using MSBuild for Silverlight, because this article from the "Die, AJAX" site does it very well. But, for comparison with the manual approach, our demo-application contains a version built with MSBuild.

Perspective FX uses also MSBuild to dynamically generate Silverlight code.

About this article

A similar article is also available for Silverlight 2.0

Author : Olivier Dewit.

History :

  • November 7th, 2009 : 1st publication (Silverlight 2.0).