Getting Started with Silveright 2 Beta 1 - Part 1

Getting Started with Silverlight 2 beta 1 – Part 1

Chaks Chandran


Silverlight 2 beta 1 was made available along with the Visual Studio 2008 Tools for Silverlight and the Silverlight SDK at MIX’08. Silverlight 2 beta 1 ships with various user controls like Textbox, Button and also supports XAML and is tightly integrated with the .NET framework. This post will give the necessary information needed in order to start developing Silverlight applications.
Tools Required

In order to develop Silverlight 2.0 beta 1 applications, the following tools are required:

  • Microsoft Visual Studio 2008
  • Silverlight 2.0 beta 1 Runtime
  • Silverlight 2.0 beta 1 SDK

Microsoft has released a unified installer which has the runtime and SDK and can be downloaded from here. Optionally the design engineers can also use the Expression Blend 2.5 Preview (please note that it is not Expression Blend 2, it’s the 2.5 Preview) to design Silverlight 2 applications.

Using Visual Studio 2008 to develop Silverlight Applications

The SDK provides the necessary tools needed to develop Silverlight applications using Visual Studio 2008. Once the SDK is installed, the Silverlight Visual Studio project templates are also installed accordingly. The Visual Studio New Project option now has Silverlight options:

The two available options are:

  • Silverlight Application
    • Used to develop normal Silverlight web applications;
  • Silverlight Class Library
    • Used to build a Silverlight library component which can be used in other websites or other Silverlight projects;

Choosing Silverlight Application will further give more options, such as:

We can either a build a web site where our Silverlight controls will be hosted or we could just generate a HTML Page to host the control. This post focuses on building Silverlight applications by generating a HTML test page and hosting the Silverlight control. Choosing the second option of generating an HTML page, Visual Studio would generate the necessary code and the files.

The Toolbox section now has a lot of new Silverlight controls.

And we also have the XAML Design and Code window.

Now, we are ready to design a small Silverlight Hello World program.
Here is the code that we have added:

Nothing fancy here. We have added a Stack Panel, an Image, a Button with a Click event, and a TextBlock. And the corresponding design looks like:

If you look at the design code, it looks very similar in building WPF applications, as both WPF and Silverlight now use XAML.
We can now insert the necessary code behind for the Button’s Click event to raise an Alert window. Note that we are building a browser window and thus we do not have the normal MessageBox, instead we have to use the Alert box.

Our Hello World Silverlight application is now ready to launch. Building and executing the application will open the application in a browser window:

The Silverlight 2 beta 1 Assemblies and Namespaces

Silverlight is a cross-browser, cross-platform plug-in which is running on top of .Net framework. So what assemblies and namespaces does Silverlight use? The references tab in the Solution Explorer does show some of the familiar names:

Are they same assemblies that are used in normal WPF applications? – The answer is NO. This is a stripped down version of the BIG .Net framework. We can also call it as a mini .Net framework built especially for Silverlight.

Where do these assemblies reside? – These assemblies reside in the Silverlight installation folder. Traverse to the following location to view the full list of assemblies

%ProgramFiles%\Microsoft Silverlight\2.0.30226.2\

These assemblies are provided by the Silverlight SDK.

How are Silverlight Applications executed?
Our whole project including the XAML and other sources are compiled into assemblies (.dlls). That’s the reason we see that every Silverlight project will always be an UserControl:

This assembly is then combined with the assemblies that our project uses and packaged into a .XAP (pronounced as ZAP) file. This XAP is then read by the Silverlight runtime in the browser and rendered accordingly. If we look into the Output while building our Silverlight project, we can see that it is indeed packaged into XAP file:

Traversing to the ClientBin folder in our project location, we can see the XAP file generated:

Let us do something interesting. As told earlier, the XAP file is nothing but an archive, in other words, a ZIP file. Copy the HelloWorld.xap to your favorite location and rename the extension .xap to .zip Now you can extract the contents from the archive and see what is inside.

What we see in the archive is the control assembly and the other assemblies that the control uses. Exploring the AppManifest file, we could see that it has a Deployment class which holds our control and other assemblies.

More about Deployment class and its’ members can be found here.
This is the end of Part 1 of building Silverlight applications with Visual Studio 2008. The next part would introduce some of the default Silverlight controls available with the SDK.
The sample application can be downloaded here:

HelloWorld-Sample


About Chakkaradeep

Chakkaradeep is the Microsoft Student Partner for University Of Otago. He writes two blogs: Chaks’ Corner and Chaks’ Geekzone Corner