What is Silverlight?
Silverlight is characterized in the following way:
Purpose - Silverlight applications allow developers to create a rich interactive application (RIA)
through graphics, media and a rich programming model (Think: Adobe’s Flash player)
Silverlight Plug-In - Web site visitors are prompted to download and install the Silverlight plug-
in. This plug-in is available for the most popular web browser software and host operating
systems. The plug-in is small (less than 2 megabytes in Silverlight 1.0) and once installed and
configured allows web pages to instantiate the Silverlight control host.
Silverlight Control Host - The control host is instantiated in a web page via JavaScript and its
purpose is to expose the Silverlight object model to application code. There are two helper files
that enable this instantiation process: Silverlight.js (supplied by Microsoft is per their license
agreement cannot be modified) and a second JavaScript file typically named
“CreateSilverlight.js” (supplied as an example implementation of how to call the method
responsible for the instantiation of the Silverlight control on the web page. The method is
named Silverlight.createObjectEx() and is described in detail later in this lesson.) Note: while
referred to as CreateSilverlight.js, when creating a Silverlight project in Visual Studio 2008 with
the Microsoft Silverlight Tools Alpha Refresh for Visual Studio 2008 Beta 2 installed, a file called
TestPage.html.js is created that serves the exact same purpose – to host the call to
createObject() or createObjectEx().
Silverlight Object Model – Similar to the .NET Framework Class Library, this set of classes are
available to your application and provide the core instructions that application developers will
use to draw and adorn shapes, render media, enable animation, provide programmatic
functionality and network access and more. Silverlight 1.0 features an object model for writing
unmanaged code. In Silverlight 1.1, an object model for writing managed code is available.
We’ll discuss the differences in Lesson 6.
Silverlight Application – Applications are created using a combination of XAML, dynamic code
(such as JavaScript) and in Silverlight 1.1, .NET static languages such as Visual Basic and C#.
Watch:
SL01_01_01 – Silverlight Overview.wmv
Silverlight 1.0 is focused on media applications that have some degree of programmability and user
interaction. When combined with Windows Media Server, Microsoft is poised to offer customers a
lower cost alternative to the Flash Player and Flash Media Server environment which costs $4500 per
license.
Although Microsoft would probably not care for my characterization, Silverlight exists as a hedge against
Microsoft’s two largest competitors.
Adobe - In recent years, Adobe (or rather, Macromedia, before it was acquired by Adobe) has made a
push to bring the Flash development environment to the desktop and to enable interaction with web
services. Adobe’s Flex platform has sought to bring applications to the desktop through its Flex software
development kit. Aspects of Flex are very similar to Silverlight, including the use of an XML-style syntax
to design application user interface and behavior. The development tools and platform has been one
way Microsoft has ensured vendor lock-in to their Windows client and server operating systems over the
past 15 years. Since the operating system business is one of Microsoft’s “cash cows”, Silverlight is one
of many strategic technologies that they use to protect their operating system market share.
Google – When Google purchased YouTube, instantly they became more than the de-facto search and
contextual ads company on the internet. They also became the largest media company on the web as
well. Microsoft desires to diversify into media and ad sales and yet again Google has gotten the jump on
Microsoft. Silverlight, and in particular Silverlight hosting through Live.com (http://silverlight.live.com)
provides free hosting for large video files. While they have not expressly stated their aim, we assume
they are doing this as a precursor to selling advertising on top of user generated video content.
Initial focus for Silverlight 1.0 has been on streaming video applications, as evidenced by
integration into Major League Baseball’s website, Fox Movies Trailer Library, Netflix streaming
and more.
Rich internet applications (RIA) that rely on back-end XML or JSON web services are clearly
targeted in Silverlight 1.1 and will be made more accessible once a solid set of controls for data
display and interaction are available. The ability for Silverlight to interact with JavaScript on the
client, as well as Microsoft Ajax, etc. to dynamically load XAML makes Silverlight more than just
a video player.
Adobe and Apple’s have created a loyal following of creative individuals who can at times be
antagonistic towards Microsoft. Microsoft is targeting this niche with its Expression toolset,
particularly Blend, which will be the primary tool used to create Silverlight assets
Developers who lack creative skills may hurt rather than help perceptions of what is possible
with this new technology
Lack of tool support (Both Visual Studio 2008 Beta 2 and Blend 2.0 Beta can create Silverlight
applications, but better tool support and integration is currently in heavy development)
Lack of a baseline set of controls (i.e., no textbox, no buttons, no drop down lists, etc.)
Currently, all of these must be custom built (although these controls will probably be ready for
the next community preview release. I’m not aware of an estimated date when these will be
available.)
The richer feature set that allows you to create meaningful applications is currently in Alpha
Very small installed user base of the Silverlight plug-in
What are the key differences between Silverlight 1.0 and 1.1?
The following key features differentiate Silverlight 1.0 RC and Silverlight 1.1 September Technology
Preview.
The Silverlight 1.1 Alpha Refresh SDK (September 2007) contains source code examples for a
Button, Scrollbar, Slider and ListBox. (See the folder called “SilverlightControlsStarterKit” which
contains a Visual Studio 2008 solution file with the source code to be used as a basis for creating
your own controls). It’s unknown if these are the basis for the actual controls, or just examples.
Grid
StackPanel
ViewBox
Additionally, Silverlight 1.1 alpha includes the ability to create UserControls. Several third party
companies and individuals have already started in earnest creating custom controls.
Improved networking stack with support for REST, RSS, JASON, POX – Allows Silverlight to
communicate with server-side web services. For more details about what each of the acronyms
above mean, see callout “Terminology”)
Enhanced, 2-way HTML/AJAX Bridge – No elaboration is available at this time.
Comprehensive and consistent base class library – Provides a scaled-down yet full featured
version of the .NET Base Class Library. Types, Collections, etc.
Support for LINQ (LINQ to Objects, LINQ to XML) – LINQ is a unified data language to express
data manipulation intent. Specific implementations of LINQ parse the query language into
instruction sets specific to the type of data that is being handled. As a staple of .NET 3.5 and
Visual Studio 2008, the an implementation of LINQ that can be used inside of a Silverlight
Terminology
RSS – Really Simple Syndication – An XML format for syndicating news feeds (See:
http://en.wikipedia.org/wiki/Really_Simple_Syndication)
JSON – JavaScript Object Notation – A simplified convention for transferring related data, such as
the properties of an object. (See: http://en.wikipedia.org/wiki/JSON)
XAML Structure
At this point we’ll move away from an overview of Silverlight in general and begin to speak in concrete
terms about XAML, the Silverlight Plug-In and the anatomy of a Silverlight application. As I mentioned in
the preface I’m assuming that you are already a seasoned developer and therefore I will move quickly
into the technical details.
Watch:
SL101_01_02 – XAML Overview.wmv
The Extensible Application Markup Language, or XAML as it is known, uses a declarative XML-style
syntax to express user interface design and behavior with the intent to keep presentation separate from
application logic. XAML is generic, and while it has been made famous by the Windows Presentation
Framework and now Silverlight it can be used in the context of other forthcoming technologies.
While XAML is a relatively new syntax, its borrows enough from XML that you should be able to
understand the basic structure of the syntax, even if you don’t completely understand how each
individual XAML element contributes to the entire application.
The following example creates a very simple Silverlight application. While you can probably guess much
of what is going on here without my explanation, I want to use this as a tangible example of several
XAML conventions:
1 <Canvas x:Name="parentCanvas"
2 xmlns="http://schemas.microsoft.com/client/2007"
3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4 Loaded="Page_Loaded"
5 x:Class="Lesson02_Canvas.Page;assembly=ClientBin/Lesson02_Canvas.dll"
6 Width="320"
7 Height="240"
8 Background="#E6E6E6"
9 >
10
11 <!-- First Rectangle Example -->
12 <Rectangle
13 Canvas.Left="50"
14 Canvas.Top="50"
15 Width="50"
16 Height="50"
17 Fill="Blue"
18 Stroke="Black"
19 StrokeThickness="3" />
20
21 <!-- Second Rectangle Example -->
22 <Rectangle
23 Canvas.Left="125"
24 Canvas.Top="50"
25 Width="50"
26 Height="50"
27 Stroke="Black"
28 StrokeThickness="3">
29 <Rectangle.Fill>
30 <SolidColorBrush Color="Green" />
31 </Rectangle.Fill>
32 </Rectangle>
33
34 <!-- Third Rectangle Example -->
35 <Rectangle Canvas.Left="50" Canvas.Top="125" Width="50" Height="50">
36 <Rectangle.Fill>
37 <RadialGradientBrush>
38 <RadialGradientBrush.GradientStops>
39 <GradientStop Offset="0.0" Color="Blue" />
40 <GradientStop Offset="1.0" Color="Green" />
41 </RadialGradientBrush.GradientStops>
42 </RadialGradientBrush>
43 </Rectangle.Fill>
44 </Rectangle>
45
46 <!-- Fourth Rectangle Example -->
47 <Rectangle Canvas.Left="125" Canvas.Top="125" Width="50" Height="50">
48 <Rectangle.Fill>
49 <LinearGradientBrush>
50 <GradientStop Offset="0.0" Color="Red" />
51 <GradientStop Offset="1.0" Color="Orange" />
52 </LinearGradientBrush>
53 </Rectangle.Fill>
54 </Rectangle>
55
56 <!-- First Textblock Example -->
57 <TextBlock Canvas.Top ="200" Canvas.Left="50" Text="Hello" />
58
59 <!-- Second Textblock Example -->
60 <TextBlock Canvas.Top ="200" Canvas.Left="100">
From this XAML source code, we can learn the following about XAML:
Declaring Objects - Use standard XML opening and closing tags to define a Silverlight object.
Containment – Some objects (like the Canvas) can contain other objects and therefore require
an opening and closing tag, and others (like the Rectangle) may not and can opt for closure in a
single tag.
<Canvas>
<Rectangle />
</Canvas>
Properties Attribute Syntax – For simple properties, you can use typical XML Attribute syntax:
Readability - Properties can be defined on multiple lines in order to make things easier to read.
See Canvas and “First Rectangle Example” from the code (above).
Content Element Syntax for Properties – In some simple cases, you can set a default property
within the context of the opening and closing tags. For example, consider the two TextBlock
examples which are equivalent:
Property Element Syntax – Some properties (usually ones that require multiple settings) can be
set like the “Second Rectangle Example”. Here, object.property “dot” notation is used to
reference the Fill property which is set to a solid green color:
Implicit Collection Syntax – The Property Element Syntax can be extended to allow a collection
of related objects to serve as the definition for a property. Consider the “Third Rectangle
Example” … it has a collection of RadialGradientBrush.GradientStops which defines the gradient
colors and positions:
<Canvas x:Name="parentCanvas"
xmlns="http://schemas.microsoft.com/client/2007"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Loaded="Page_Loaded"
…
There are other small nuances to XAML syntax, but this covers most of what you’ll encounter. For more
details, see the article “XAML Syntax Overview” at http://msdn2.microsoft.com/en-
us/library/bb412392.aspx
A Web page to host the Silverlight control – This can be any file that delivers HTML, such as a
.html file, a .aspx page, a .php page, etc. There are two key items that must be present to load
the references to the external files (Silverlight.js and the .js file that contains the
Silverlight.createObject() method call which is usually createSilverlight.js when using the file
from the SDK, or TestPage.html.js when using Visual Studio 2008 Beta).
Important:
There are “breaking changes” in Silverlight 1.1 Alpha Refresh (i.e., changes to Silverlight 1.1 Alpha
Refresh that will break older code examples found strewn around the internet, and even in Microsoft’s
own documentation). Therefore, if you’ve used a previous version of the SDK you will have to fix several
small issues in your code. For a list of breaking changes, see the document “Changes from May 2007
Alpha.docx” after unzipping the Silverlight 1.1 Alpha Refresh SDK.
source Name of XAML file to load … when used programmatically, can be used to load
XAML in different forms, such as when embedded within the HTML, or when
delivered by a web service call, etc.
parentElement A reference to the HTML element (usually a DIV tag) that will host the Silverlight
control
properties Contains a listing of name / value pairs … see table 2 for a list of properties that
can be modified
events Contains a listing of name / value pairs that represent the event name and the
name of the JavaScript function that will handle that event … see table 3 for a
list of events that can be handled
initParams Contains a listing of name / value pairs that can be used as user defined
initialization value … think “command line parameters”
context If you have multiple Silverlight controls loaded into a single web page the
context will define which control’s onLoad event handler function
Width Width of the Silverlight host control … can be a percentage … can clip the width
of the root Canvas
Height Height of the Silverlight host control … can be a percentage … can clip the
height of the root Canvas
isWindowless When set to true, allows the canvas to be transparent. (To achieve this effect,
must set the background property to “Transparent” or “#000000”)
frameRate Display frames per second (fps) … important when working with animation.
When set higher, makes motion smoother, but at the risk of system
performance. Max is 64 fps.
True:
False (default):
In 1.1, there is no in-place install version. Regardless of this setting, the end
user will only see this:
version Which version of the Silverlight plug-in is the XAML and script targeted for?
Current valid settings are “1.0” or “1.1”.
ignoreBrowserVer Default is False, which performs a check to ensure that the browser is able to
run the Silverlight application. True skips this check.
enableHtmlAccess Default is True, which means that the Silverlight application can interact with
the DOM of the web page hosting the Silverlight host control.
onLoad Sets the event handler for the event that is raised when the XAML has finished
loading
onError Sets the event handler for the event that is raised when the Silverlight
application encounters an otherwise unhandled exception.
Silverlight.createObject( Silverlight.createObjectEx({
"AgControl1", id:'AgControl1',
{ properties: {
width:'640', width:'640',
height:'480', height:'480',
inplaceInstallPrompt:false, inplaceInstallPrompt:false,
background:'white', background:'white',
isWindowless:'false', isWindowless:'false',
framerate:'24', framerate:'24',
version:'1.1' version:'1.1'
}, },
{ events: {
onError:null, onError:null,
onLoad:null onLoad:null
}, },
null); context:null});
Alternatively, the developer can set inplaceInstallPrompt to false, which displays the longer icon. The
difference? Notice the hyperlink to the EULA and the notice that by clicking the icon the user is
accepting the EULA and installing the plug-in immediately.
In some cases, the browser will need to be restarted anyway, so it’s a rather moot point whether to
keep the user on your page or send them to Microsoft’s Silverlight site.
See the project folder Lesson01_XAML for the complete implementation of the following example.
Important:
When using the inline XAML technique, there is a known work around for FireFox. By default, Visual
Studio 2008 puts the following line into TestPage.html:
However, this causes a known FireFox bug to occur which will not display your Silverlight application.
You can remove this line, or if setting the doc-type is important to you, there are several more creative
workarounds available. Perform a search for “firefox inline xaml” for more details.
Additionally, while Silverlight 1.0 has been officially released, many of the interesting programmatic
functionality will appear in Silverlight 1.1. Therefore, this textbook is targeting Silverlight 1.1. There are
two caveats in this regard:
Silverlight 1.1 is in “Alpha” and is currently undergoing heavy development. Therefore, the
version of the software you are using and this document may become out of sync causing
examples to no longer work correctly. Please check back on LearnVisualStudio.NET for updates
to this document.
This textbook requires the installation of software that is currently in beta. After the official
release of Visual Studio 2008 the free “Beta” will no longer exist. Therefore, you will need to
acquire a license to use Visual Studio 2008 (as well as other tools we might use, such as Blend)
to continue learning from this resource. If the Visual Web Developer 2008 Express Edition
allows for creation of Silverlight applications (via an add-in of some sort) then I will use that to
appeal to the greatest audience size.
Install the tools from the following locations in the following order:
Note:
I use Alcohol 120% for opening an ISO file and installing it directly onto a desktop machine’s OS.
Otherwise, when installing within a Virtual Machine, both VMWare and VirtualPC can both treat an .iso
as a virtualized DVD drive.
There are other optional tools that you might find helpful as I’ll refer to these throughout the textbook
and labs.
The Silverlight 1.1 Alpha Refresh SDK is useful for a clean version of the CreateSilverlight.js and
Silverlight.js (assuming you’ll not be using Visual Studio or Blend). It also has documentation, as well as
the controls starter kit mentioned previously in the section titled “What are the key differences between
Silverlight 1.0 and 1.1?”
Microsoft Expression Blend 2.0 will most likely be where most designers and developers prefer to build
their XAML since it has tools to draw shapes, place controls, edit timelines and so forth. Blend can open
a Visual Studio project and vice versa to enable the interaction between designer and developer.
http://channel9.msdn.com/ShowPost.aspx?PostID=338599
The third method is what we’ll use throughout this textbook. There are several benefits to embracing
this development environment:
Watch:
SL101_01_03 - Silverlight Projects in Visual Studio 2008.wmv
Creating a new Silverlight project is as easy as selecting the appropriate template from the Silverlight
project type in the New Project dialog.
From the previous discussions earlier in this lesson, the contents of a new Silverlight project should be
straightforward. As described earlier, note the presence of the TestPage.html.js file which is depicted
as being related to the TestPage.html (again, will need to select the Show All Files button to see this file).
This file replaces the createSilverlight.js file that is described in most documentation currently available
In Lesson 2 “Silverlight Graphical Elements Basics”, most of the work we’ll perform will be exclusively in
the Page.xaml file because we’ll only be writing XAML to display shapes and other elements on screen.
In Lesson 3 “Scripting and Mouse Events using Unmanaged APIs” and beyond, most of our work will be
in the TestPage.html where we’ll write JavaScript between the <script> tags in the <head> section of the
HTML document.
Beginning in Lesson 6 and 7, “Writing Managed code with Silverlight 1.1 and Visual Basic” and “Writing
Managed code with Silverlight 1.1 and C#” respectively, we’ll begin writing managed code inside the
context of the Page.xaml.vb and Page.xaml.cs files, respectively.
When selected, Visual Studio 2008 will ask which Silverlight application to link to.
Once the two projects are linked, the assemblies that contain managed Silverlight event handlers will be
automatically copied to the \ClientBin directory of the ASP.NET project. Whenever a new Build of the
Silverlight project is created, the output assemblies are updated. Additionally, changes made in the
Page.xaml file will be copied between the two projects.
Watch:
SL101_01_04 - Linking a Silverlight Project to an ASP.NET Project in Visual Studio 2008.wmv
1. In IIS6, right click your website site in the left-hand pane and select “Properties …” from the
context menu
2. In the Properties dialog, select the HTTP Headers tab, then click the “MIME Types …” button.
4. In the MIME Type dialog, enter “.xaml” as the Extension and “application/xaml+xml” as the
MIME type. Click Ok.
Exercises
Review Questions
1. Describe the key differences between Silverlight 1.0 and Silverlight 1.1.
2. What is the purpose of the Silverlight.js and createSilverlight.js files? What important method
call is contained in the createSilverlight.js file?
3. In XAML, what is the purpose for the Canvas?
4. Does all XAML code have to be in a file with the extension .xaml? Explain.
5. What methods or tools can be employed to compose Silverlight applications?
Case Scenarios
1. Since you are an early adopter, you want to create a smooth installation process of the
Silverlight plug-in for your users. What factors will you have to weigh in deciding how to best
implement your application to reduce the amount of friction required for them to utilize it?
2. The marketing department enlists your help to conceptualize and built a new rich interactive
application for a brand new product line. The marketing department needs video
demonstrations as well as copious amounts of animation and interactivity to convey the
message about the product’s features and to build excitement. The timeframe is short and this
will be a high profile project within the company. What would be your technology
recommendation?
To watch this procedure, watch video: “SL101_01_05 – Lab 1 -Setting up a Silverlight Development
Environment.wmv”
To set up a development and testing environment suitable for the examples in this workbook and lab
manual, complete the following procedure:
1. Download the Visual Studio 2008 Beta 2 ISO file – You can download from the following
location: http://www.microsoft.com/downloads/details.aspx?familyid=B98A61BA-99B0-40B7-
AB6E-5386A2B94217&displaylang=en
The actual file name will be: en_visual_studio_2008_beta_2_professional_edition_dvd.iso,
however I’ll simply refer to it as the Visual Studio 2008 Beta 2 ISO file in this lab.
2. Download and install Alcohol 120% - The trial version will work for 30 days with a “nag screen”.
This utility will allow you to mount an ISO file so that Vista will see it as another DVD drive in
your system. Download from http://www.alcohol-soft.com. (You can use any other software or
technique to open or mount an IMG file.) Note: installation of Alcohol 120% requires a reboot.
May want to wait until after you finish downloading the Visual Studio ISO file. Caution: Alcohol
120% will affect system drivers. Use at your own risk.
3. Open ISO in Alcohol 120% - Once installed, select File | Open … to display the Open dialog.
Navigate to the download location of the Visual Studio 2008 Beta ISO. Select the file and click
the Open button. The File should now be displayed in the main area of Alcohol 120%.
4. Mount ISO - Right-click the Visual Studio 2008 Beta ISO in the main area of Alcohol 120% and
select Mount On Device. In some cases, you may see Mount On Device and a sub-menu option
with the available virtual drives which I’ll refer to as {Your Virtual Drive Letter Here}. Once you
click either option, in the lower pane, you should see the file location of {Your Virtual Drive
Letter Here} change to the file location you selected in step 3.
5. Install Visual Studio 2008 Beta 2 – Typically in the previous step, the Setup screen for Visual
Studio 2008 Beta 2 will appear. (Alternatively in Vista, the Autoplay dialog will appear. Select
the option “Run autorun.exe” to display the Visual Studio 2008 Beta 2 Setup screen.) If
autoplay is not enabled, use Windows Explorer to locate {Your Virtual Drive Letter Here} and
navigate and double-click the autorun.exe file on the virtual drive. Click “Install Visual Studio
2008” link in the Setup dialog. You can agree to all the typical dialogs, or customize the
installation to your liking as long as you don’t remove Visual Basic or Visual C# from the
installation options. Note: Installation will require a reboot. If you are using Alcohol 120%, after
reboot you may encounter a problem … “The Windows Installer package: {Your Virtual Drive
Letter Here}\vs_setup.msi could not be opened”. Simply open Alcohol 120%, and once its