Getting Started with Silverlight
One of the most talked about aspects of Silverlight is its undeniable technical accomplishments. In an age of 50 megabyte downloads, Microsoft managed to squeeze large parts of WPF and a mini CLR into a tiny browser plug in, currently under 5MB. Not only that, but it runs in the three major browsers (Firefox, Safari and IE) and more amazingly on the three major operating systems, with Microsoft supporting Windows and Mac OS X and Novell Mono project providing a Linux runtime.
This article explains a little of the history behind Silverlight and what you can use it for. You will see how easy it is to get started, and then you will actually practice using a free InnerWorkings coding challenge featuring a sample app that demonstrates how to use and transform video in Silverlight.
Windows Presentation Foundation Everywhere (WPF/E) was announced two years ago at the 2005 PDC (Professional Developer Conference), but kept a low profile for a long time. At Mix 07 in May however, the first 1.0 beta was released under the Silverlight name, along with (a little confusingly) a preview of the 1.1 release.
Version 1.1 however still in technical preview stage is what got everyone excited. Much more than a minor revision, this adds a bite sized CLR to the plug in, capable of executing compiled C#, VB, Python and Ruby code. Though it is cut down, the Core CLR still provides a just in time compiler and works with the exact same assemblies as the full blown desktop CLR. If you can restrict yourself to the smaller set of classes included with Silverlight CLR, there is complete compatibility between the two, with no recompilation necessary.
How does Silverlight work?
From a user point of view, the experience is designed to be as smooth as possible. On visiting a Silverlight enabled site for the first time, a message appears inviting the user to download the plug in from the Microsoft site. The install is quick and painless, although it can require a browser restart. After that, any Silverlight content is displayed automatically and the plug in even updates itself as required.
The actual scene is defined in XAML, an XML based format where elements and attributes correspond to .NET objects and their properties. This is the same XAML that powers WPF, just with some of the elements missing.
What's left, at least in Silverlight 1.0, is enough to draw shapes, images, text and video. Some of the flexible layout options from WPF (like the Grid, DockPanel and StackPanel) are missing, along with any kind of prebuilt UI control there are no buttons, menus, listboxes or anything. Though you can use HTML controls instead, this is a major shortcoming, and the Silverlight team have promised some of these controls for a future release of version 1.1, along with a set of panels to make layout easier.
To build an app that does more than just look pretty, you can attach event handlers to Silverlight objects and write code that manipulates the scene, starting animations, controlling video or audio playback and updating the properties of objects. Silverlight really does behave like a mini WPF, so if you are familiar with its big brother (or Windows Forms or ASP.NET for that matter), the programming model is easy to follow.
Other than the XAML script tag, the snippet contains an object tag that actually creates an instance of the Silverlight control, passing it a reference to the XAML it should use (in this case preceded by the # symbol to specify the ID of the HTML element with the content).
<script type="text/xaml" id="xamlContent">
<TextBlock Text="Hello, Silverlight!" />
<object id="silverlightObj" type="application/x-silverlight" width="200" height="200">
<param name="source" value="#xamlContent" />
Displaying video with Silverlight
One of the strengths of WPF (and so Silverlight) is its composability: objects can be nested within each other easily to create composite objects. A button, for example, does not have to contain just a text label, it could contain an image as well (or a video, or a tiny interactive game, or whatever you want). While many of these uses are not, well, useful, the simplicity that composability affords helps with all sorts of more common scenarios. If you want to display an image or video inside a circle or a rounded rectangle, you do not have to set some obscure property of the image, you just pop it inside a suitably shaped container.
In the same way, Silverlight animation and transformation features can apply to video as easily as primitive shapes. This makes it easy to render video on objects that can be dragged around, or to zoom into or clip video. The key to this is the VideoBrush, which can be used to paint the interior of a shape with the output of a MediaElement. The MediaElement itself can then be hidden, leaving only the VideoBrush painted shape visible.
Try it out
Silverlight is one of those technologies where words alone can not possibly do it justice. Have a look at the tutorials at http://silverlight.net/ to find out more, or skip ahead to http://silverlight.net/showcase/ to see what can be done.
Next, download the free Silverlight challenges from InnerWorkings and get to grips with some real Silverlight projects. You will learn more about setting up Silverlight in your own apps, as well as practice using a VideoBrush to render and control video clipped and magnified according to your own specifications.
What's an InnerWorkings coding challenge?
An InnerWorkings coding challenge is a sample of code in Visual Studio that has some key pieces missing. Each challenge includes selected reference links chosen specifically to help you find out how to fill in the blanks, complete the sample app, and learn about a new technology at the same time. Once you are finished, InnerWorkings Developer automatically checks your code so you can be sure you have solved the challenge correctly and that you really understand what you have learned.
Our coding challenges are designed to take you to the heart of the technology you want to learn more about, focusing on the most important, practical features. Because everything has been set up for you, you can dive straight in and start coding.
InnerWorkings has a library of hundreds of challenges on diverse topics from ASP.NET to Windows Communication Foundation. For more information, have a look at our catalog.