Original URL: http://www.theregister.co.uk/2007/11/05/get_started_with_silverlight/
Hands on There has been much hype and more than a little confusion during the last year or so surrounding Microsoft Silverlight.
Is it a Flash killer? Is it a designer tool or a developer tool? Surely it's for streaming videos? Is it a cross platform, browser-based version of Microsoft's Common Language Runtime (CLR)?
Microsoft will go some way towards clearing up the confusion this week during sessions for developers interested in using Silverlight to build rich internet applications (RIAs) attending the company's TechEd and DevConnections shows in Barcelona, Spain, and Las Vegas, USA.
Silverlight doesn't actually fit neatly into any existing category. Instead, it's an exciting and important new technology that could help to change the way we build Web applications.
Silverlight 1.0, released earlier this year offers only a JavaScript programming model. That will change with the next major version, Silverlight 1.1 that is currently in Alpha and will also feature Microsoft's CLR. This will bring the ability to program RIAs and media-based applications using .NET languages like C#, VisualBasic.NET and IronPython, while providing for smoother collaboration between developers and designers.
In this introductory piece I'll show how you build a simple Silverlight application. I'll take you through the basics of a "Hello World" application and give you enough information to start to explore the wide range of possibilities for yourself.
Silverlight is a browser-based runtime environment for building rich media applications. It's a browser plug-in that effectively extends the browser's Document Object Model (DOM) by adding graphical, multimedia and presentation facilities that can be defined in eXtensible Application Markup Language (XAML). It can be scripted using JavaScript.
Silverlight works with Firefox and Safari on the Mac and Firefox and Internet Explorer on Windows. You don't need an ASP.NET application or a Windows Server to deliver a Silverlight application, meaning that PHP programmers using a Linux and Apache stack can just as easily incorporate Silverlight content.
Whichever platform you use to develop, you'll need to install the Silverlight 1.0 runtime for your browser. If you visit a site that uses Silverlight, and you don't yet have the Silverlight runtime installed, you'll be directed to an installation process. The runtime is surprisingly small (less than 2MB). The Silverlight home page (http://www.microsoft.com/silverlight/) lets you install the plug-in and see demonstrations. The Microsoft Silverlight 1.0 Software Development Kit (SDK) is available along with other resources for Windows from MSDN (http://go.microsoft.com/fwlink/?LinkID=88923).
A Silverlight application typically consists of XAML markup and JavaScript code. You also need an HTML file to call the application. We'll start with four files:
The HTML contains references to the JavaScript files, provides a DIV tag to host the Silverlight control and then calls the JavaScript code to start the Silverlight content:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
The Silverlight.js file is a standard file, distributed as part of the Microsoft Silverlight 1.0 SDK and widely available elsewhere on the Web.
The CreateSilverlight.js file defines the createSilverlight() function that is called from the HTML. It finds the DIV tag that will host the Silverlight control and provides the glue to load and start your XAML content (defined in Scene.xaml).
function createSilverlight()
{
Silverlight.createObjectEx({
source: 'Scene.xaml',
parentElement: document.getElementById('SilverlightPlugInHost'),
id: 'SilverlightPlugIn',
properties: {
width: '400',
height: '400',
background:'#ffffff',
isWindowless: 'false',
version: '1.0'
},
events: {
onError: null,
onLoad: null
},
context: null
});
}
All the magic happens in Scene.xaml – this is where we define our Hello World application.
<Canvas xmlns="http://schemas.microsoft.com/client/2007" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<TextBlock FontFamily="Times New Roman"
FontSize="70"
FontStyle="Italic"
Foreground="Red"
Text="Hello World" />
</Canvas>
I'll explain the thinking behind XAML in a moment, but for now save these four files to a new directory and load the HTML into your browser. If you are running locally, your browser may warn you about Active X controls and block the content (this isn't the case when you run from a web server). Unblocking should give you a page like this:

XAML first appeared with the Windows Presentation Foundation (WPF) to describe the various graphical and multimedia assets along with their placement, characteristics and behaviours. XAML decouples presentation information from application logic allowing files to be shared between developers and designers. XAML is at the heart of the fledgling Microsoft Expression tools for design and will be prevalent in the forthcoming Visual Studio 2008.
A XAML file typically starts with a canvas, onto which you can place various graphical objects. In the example above, we added a TextBlock, but we could have equally used a Rectangle, Ellipse, Polygon or any one of a number of predefined elements. The MediaElement even provides access to video playback.
Animations can be specified by creating a Storyboard and a number of event triggers. All this can be extended by developers using JavaScript code.
Now that we have a basic framework in place, we can start to experiment and add features just by changing the scene.xaml file.
You can easily plug in your own video content – here I've just taken one of the videos that come with Windows, renamed it to MyVideo.wmv and copied it into the project directory.
You can reference the video with a MediaElement and then use it in a VideoBrush.
<Canvas xmlns="http://schemas.microsoft.com/client/2007" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<MediaElement Name="Video" Source="MyVideo.wmv" Opacity="0" />
<TextBlock FontFamily="Times New Roman"
FontSize="70"
FontStyle="Italic"
FontWeight="Bold"
Text="Hello World" >
<TextBlock.Foreground>
<VideoBrush SourceName="Video" />
</TextBlock.Foreground>
</TextBlock>
</Canvas>
Now when you run the HTML the video content plays back, masked by the text.

There's nothing to stop you taking this example, extending it and incorporating it into your own site, but that's only part of the Silverlight story – with the Silverlight Streaming Service (currently in Alpha), Microsoft is also offering to host your application. Currently, that's free of charge.
You can sign up for a Silverlight Streaming account here (http://silverlight.live.com), and host up to 4Gb of content including video. Individual videos can be no larger than 22Mb – equating to about 10 minutes at 300Kbps.
The current Silverlight Streaming service gives you access to a global, high performance, high availability content distribution network - the same content delivery network that recently carried the video clips and demonstrations for the launch of Microsoft's Halo 3.
You start by creating a manifest.xml file that tells the streaming service which XAML file to load and run:
<SilverlightApp>
<version>1.0</version>
<source>Scene.xaml</source>
</SilverlightApp>
Next you create a ZIP file that includes this manifest file, the Scene.xaml file and the video MyVideo.wmv. You can also include JavaScript files that add functionality, but we'll keep this demonstration application simple.
On Windows, multiple select the files, right click and select Send To Compressed (zipped) folder - you can use any of the popular ZIP compression programmers, but you need to make sure that the manifest file appears in the top level root of the zip file - zipping the parent directory will not work.

Having signed up, you simply log in to Silverlight using a Windows Live ID - something you must use - and then click Manage Applications. Select Upload a Silverlight Application, specify a name and browse for your ZIP file. Click Upload. Depending on your connection speed, this may take a while. If all goes well, you will be presented with a page that allows you to test your application. You will also be given sample HTML and JavaScript that you can cut and paste into your own web site to host the application.
To create more serious XAML applications you really need a good XML editor. The Microsoft Silverlight 1.0 SDK includes an add-in for Visual Studio 2005 that provides context-sensitive XAML editing and IntelliSense. The forthcoming Visual Studio 2008 will include a XAML based Graphical User Interface builder.
Expression Encoder is available as a free trial download and allows you to edit and manipulate video and audio clips. Available here (http://www.microsoft.com/expression/products/overview.aspx?key=encoder), Expression Encoder – part of the Expression suite - will generate XAML video player applications in a variety of styles for use with Silverlight Streaming with an appropriate manifest.xml file.
So to wrap up, Silverlight is a browser-based plug-in that extends programming in DOM using XAML to program video and graphics. Using XAML, you can construct a basic framework that's relatively simple to customize and extend while the Silverlight Streaming Service lets you harness Microsoft's content distribution network to deliver media on a global scale. These are early days for Silverlight, but the platform shows great promise. ®
Rob Blackwell is director of R&D for Active Web Solutions (http://www.aws.net) who worked with the Royal National Lifeboat Institute (RNLI) to build its award winning Sea Safety tracking system. You can keep up with Rob here (http://www.robblackwell.org.uk/).
Why is Ruby on Rails so darn slow? (21 April 2008)
http://www.theregister.co.uk/2008/04/21/bray_ruby_rails/
Microsoft teases mobile developers with 'big' Silverlight deals (17 April 2008)
http://www.theregister.co.uk/2008/04/17/silverlight_mobile/
Google sneaks under standards radar (16 April 2008)
http://www.theregister.co.uk/2008/04/16/google_de_facto_geographic_standard/
The trinity of RIA security explained (8 April 2008)
http://www.theregister.co.uk/2008/04/08/ria_security/
Open AJAX frameworks not fit for 'power users' (28 March 2008)
http://www.theregister.co.uk/2008/03/28/ajax_analyst_reversal/
Dump IE 6 campaign runs afoul of dump IE 6 campaign (26 March 2008)
http://www.theregister.co.uk/2008/03/26/internet_explorer_six_save_developers/
Microsoft cozies to Eclipse, no tongues though (19 March 2008)
http://www.theregister.co.uk/2008/03/19/microsoft_eclipse_swt_project_wpf/
Quick scripting for .NET with IronPython (13 March 2008)
http://www.theregister.co.uk/2008/03/13/ironpython_part_one/
Ballmer pledges PHP love in Microhoo future (6 March 2008)
http://www.theregister.co.uk/2008/03/06/ballmer_yahoo_php/
Microsoft indemnifies Novell Moonlight users (6 March 2008)
http://www.theregister.co.uk/2008/03/06/microsoft_moonlight_patent_protection/
Silverlight 2.0 data and web services explained (6 March 2008)
http://www.theregister.co.uk/2008/03/06/silverlight_two_data_services/
Microsoft jump starts IE 8 with community push (5 March 2008)
http://www.theregister.co.uk/2008/03/05/ie8_standards_support/
Microsoft architecture chief 'clarifies' online formula (5 March 2008)
http://www.theregister.co.uk/2008/03/05/ozzie_microsoft_services/
Database spin on Sun's Python hires (4 March 2008)
http://www.theregister.co.uk/2008/03/04/sun_python/
Nokia boards Silverlight express (4 March 2008)
http://www.theregister.co.uk/2008/03/04/s60_silverlight/
Microsoft's LAMP answer arrives in pieces (27 February 2008)
http://www.theregister.co.uk/2008/02/27/windows_server_2008_launch/
Cut to the Web Server Core: Windows Server 2008 (27 February 2008)
http://www.theregister.co.uk/2008/02/27/windows_2008_web_server_core_review/
Microsoft and Adobe jockey on rich applications (25 February 2008)
http://www.theregister.co.uk/2008/02/25/microsoft_adobe_ria_internet_explorer/
Build a directory service for web-based services (18 February 2008)
http://www.theregister.co.uk/2008/02/18/ldap_php/
Ruby project yields to Microsoft (5 February 2008)
http://www.theregister.co.uk/2008/02/05/ruby_dot_net_ends/
Johnson pours fourth on SpringSource (31 January 2008)
http://www.theregister.co.uk/2008/01/31/rod_johnson_springsource/
Visual Studio beats SQL Server in 2008 stakes (30 January 2008)
http://www.theregister.co.uk/2008/01/30/visual_studio_2008_launched/
Inside the Windows 2008 stack experience (24 January 2008)
http://www.theregister.co.uk/2008/01/24/project_watch_one/
Sun's JavaFX tools to interop with Adobe (24 January 2008)
http://www.theregister.co.uk/2008/01/24/javafx_tools_adobe/
Major HTML update unveiled (22 January 2008)
http://www.theregister.co.uk/2008/01/22/html_five_preview/
Build a Silverlight media player (9 January 2008)
http://www.theregister.co.uk/2008/01/09/silverlight_media_application_service/
Emacs diet for Visual Studio? (3 January 2008)
http://www.theregister.co.uk/2008/01/03/emacs_dot_net/
Build your PDF and Excel reports with PHP (10 December 2007)
http://www.theregister.co.uk/2007/12/10/pdf_excel_reports_with_php/
Gates' spontaneity highlights IE data gap (10 December 2007)
http://www.theregister.co.uk/2007/12/10/gates_ie8_fire_drill/
Microsoft wriggles on Volta concerns (7 December 2007)
http://www.theregister.co.uk/2007/12/07/volta_technology_preview/
MS Explorer foundering after UFO strike (23 November 2007)
http://www.theregister.co.uk/2007/11/23/ms_explorer_ufo_sinking_ship_not_software/
Reg Dev wants your Big Three for 07 and 08 (20 November 2007)
http://www.theregister.co.uk/2007/11/20/old_new_year_picks/
Microsoft lets slip Visual Studio 2008 (19 November 2007)
http://www.theregister.co.uk/2007/11/19/visual_studio_2008_access/
Tune your service oriented applications with SQL (19 November 2007)
http://www.theregister.co.uk/2007/11/19/database_performance_management_soa/
Get into data with Groovy (13 November 2007)
http://www.theregister.co.uk/2007/11/13/groovy_mysql/
Project Astoria a hit at TechEd (13 November 2007)
http://www.theregister.co.uk/2007/11/13/project_astoria/
Microsoft talks Sync and BI for SQL Server 2008 (5 November 2007)
http://www.theregister.co.uk/2007/11/05/sql_server_2008_sync_bi/
Visual Studio 2008 release by end November, Sync Framework announced (5 November 2007)
http://www.theregister.co.uk/2007/11/05/visual_studio_2008_november/
Buzzword bingo for Microsoft's Oslo (30 October 2007)
http://www.theregister.co.uk/2007/10/30/microsoft_oslo/
Considering Microsoft’s 'rift with the web' (23 October 2007)
http://www.theregister.co.uk/2007/10/23/microsoft_dot_net/
Of opposable thumbs and software engineering (19 October 2007)
http://www.theregister.co.uk/2007/10/19/evolution_software_development/
We'll beat Microsoft and Sun, says Adobe's chief software architect (18 October 2007)
http://www.theregister.co.uk/2007/10/18/adobe_air_platform-ambitions/
Curl mounts Silverlight and AIR challenge (10 October 2007)
http://www.theregister.co.uk/2007/10/10/curl_open_source/
Open .NET challenge for iPhone development (5 October 2007)
http://www.theregister.co.uk/2007/10/05/openmoko_iphone/
Microsoft targets developers with 'open' license (4 October 2007)
http://www.theregister.co.uk/2007/10/04/microsoft_dot_net_opensource_license/
Don't get stuck with AJAX, Oracle warns (25 September 2007)
http://www.theregister.co.uk/2007/09/25/oracle_ajax_jsf/
MS shines Silverlight into thin AIR (and kicks 'beaten up' Google Gears) (17 September 2007)
http://www.theregister.co.uk/2007/09/17/microsoft_loves_silverlight_slams_adobe_air_google_gears/
© Copyright 2008