Getting Started with Silverlight
- Embedding the Silverlight Control Manually
- Letting Silverlight.js Handle the Dirty Work
- Understanding Your Hosting Options
- Interacting with the Silverlight Control Programmatically
- Conclusion
IN THIS CHAPTER
- Embedding the Silverlight Control Manually
- Letting Silverlight.js Handle the Dirty Work
- Understanding Your Hosting Options
- Interacting with the Silverlight Control Programmatically
Despite all the wonderful things you can say about HTML, CSS, and JavaScript, I think most people doing a lot of web-based development would agree that they form a pretty poor environment for developing modern sites and applications. If you care about your content working on most web browsers (or even just Internet Explorer and Firefox), accommodating their differences can be maddening. Many techniques and JavaScript libraries have been developed and shared over the years that can reduce this frustration, but none of them are silver bullets.
In addition to browser differences, the graphical capabilities of HTML are too limiting for many user experiences that people want to create. Drawing a simple line, incorporating video, and a number of other things are extremely difficult or impossible with HTML alone. It's not that these technologies were poorly designed, but simply that they were designed for hyperlinked documents rather than the extremely rich presentations that most people want to create on the Web these days.
Considering these issues, it's no wonder that Adobe Flash has been so successful. Whether someone wants to create a professionally designed website, an online game (or any number of other applications), or even a simple advertisement, Flash has been a natural choice for escaping the limitations of HTML. If you doubt the pervasiveness of Flash, try this experiment: Think of a brand of food you eat, and then navigate to the brand's website. Chances are you'll find Flash content at your destination. (I just tried pepsi.com, doritos.com, and oscarmayer.com, and all three are using Flash at the time of writing.) The Flash development experience leaves much to be desired, however. Flash (the runtime environment, as well as the tool) suffers from the same basic problem as HTML: Many people are trying to use it for creating rich applications, but it was originally designed for something else (in this case, simple animations).
This is why the introduction of Silverlight is so exciting. A promising alternative to Flash, Silverlight enables the creation of rich web content and applications using a lightweight add-on that is friendly to both designers and developers. Yes, the first version of Silverlight is primitive in areas, but it's a true development platform based on concepts and APIs introduced with Windows Presentation Foundation (WPF) in 2006 and in development for many years prior. And, unlike just about any software that has come out of Microsoft, Silverlight is a small download! Version 1.0 is less than 1.5MB, so users who don't have it can get it pretty quickly when browsing to Silverlight content. (By default, Silverlight also automatically updates to later versions when they are available.) Silverlight might just be the silver bullet many designers and developers have been waiting for.
Silverlight 1.0 applications are created with a mixture of XAML (Extensible Application Markup Language), HTML, and JavaScript, so they are easy to integrate into existing web content and compatible with popular Asynchronous JavaScript and XML (AJAX) libraries and techniques. XAML is an XML-based declarative language described in depth in the next chapter. In typical Silverlight applications, a XAML file contains a hierarchy of visual elements that must be rendered on the screen. Silverlight parses the XAML content on initialization, and then renders the content as appropriate.
Embedding the Silverlight Control Manually
Silverlight, just like Adobe Flash, is a web browser add-on. It's a pair of components—one for Internet Explorer (an ActiveX control), and one for all other supported browsers (a Netscape plug-in)—but this is an invisible implementation detail to make things "just work" regardless of the host browser. The standard way for web pages to take advantage of an add-on—whether Silverlight, Flash, or another—is with the OBJECT HTML element.
Listing 1.1 contains a simple web page for a fictional "Great Estates" housing development that embeds a Silverlight logo at the top using the OBJECT element.
Listing 1.1. A Web Page with Embedded Silverlight Content
<html> <head> <title>Great Estates</title> </head> <body style="background:blue"> <!-- A Silverlight-based logo: --> <object type="application/x-silverlight" id="silverlightControl" width="390" height="100"> <param name="background" value="Yellow"/> <param name="source" value="Chapter1.xaml"/> </object> <p style="font-family:Tahoma; color:white"> An idyllic new community located high on a hill and offering captivating waterfront views. Tailored to meet both the needs of upsizing and downsizing buyers, Great Estates offers custom quality architecture and design at an affordable price point. </p> </body> </html>
The id, width, and height attributes on the OBJECT element work the same way as on elements such as DIV, TABLE, and so on. For example, width and height can be specified in absolute pixel values or as a percentage. The type attribute refers to the MIME type of the add-on content. The Silverlight add-on is invoked by the host browser for any content of type application/x-silverlight.
The Silverlight add-on supports several custom parameters, covered later in the "Understanding Your Hosting Options" section. In this example, the background parameter is set to fill the 390x100 region with the color yellow, and the source parameter is pointing to a separate XAML file containing the content to be rendered on top of the yellow background. This XAML file, Chapter1.xaml, is shown in Listing 1.2.
Listing 1.2. Chapter1.xaml—A XAML File Containing a Logo
<Canvas xmlns="http://schemas.microsoft.com/client/2007"> <MediaElement Name="video" Source="Lake.wmv" Opacity="0" IsMuted="true"/> <!-- A circle containing a live video: --> <Ellipse Width="100" Height="100"> <Ellipse.Fill> <VideoBrush SourceName="video"/> </Ellipse.Fill> </Ellipse> <!-- Two pieces of text: --> <TextBlock FontFamily="Georgia" Foreground="Blue" FontStyle="Italic" FontSize="40" Canvas.Left="125" Canvas.Top="20" Text="Great Estates"/> <TextBlock Foreground="Blue" Canvas.Left="110" Canvas.Top="70" Text="Luxurious Living at an Affordable Price"/> <!-- Curves and a line: --> <Path Stroke="Red" StrokeThickness="4"> <Path.Data> <PathGeometry> <PathFigure StartPoint="0,65"> <ArcSegment SweepDirection="Clockwise" Size="2,2" Point="25,65"/> <ArcSegment SweepDirection="Clockwise" Size="2,2" Point="50,65"/> <ArcSegment SweepDirection="Clockwise" Size="2,2" Point="75,65"/> <ArcSegment SweepDirection="Clockwise" Size="2,2" Point="100,65"/> <LineSegment Point="390,65"/> </PathFigure> </PathGeometry> </Path.Data> </Path> </Canvas>
This XAML file defines a logo containing two lines of text, some vector artwork, and even a live video cropped by a circle! Don't worry about the syntax of the XAML file for now. The next chapter covers everything you need to know about XAML syntax, and the various Silverlight elements (Canvas, MediaElement, Ellipse, and so on) are covered throughout the remainder of the book.
Figure 1.1 displays the web page defined by Listings 1.1 and 1.2. Most web pages probably would make the Silverlight content blend in better by giving the OBJECT element a matching background, but for this example, the yellow background helps to highlight the area of the page rendered by Silverlight.
Figure 1.1 Silverlight content manually hosted in a web page with the OBJECT element.
Of course, the Great Estates web page only resembles what's shown in Figure 1.1 if the viewer has the Silverlight add-on installed. Without the add-on, the page looks similar to Figure 1.2 (depending on which browser you use).
Figure 1.2 Listing 1.1 doesn't look good when the Silverlight add-on is missing or disabled.
Fortunately, there's a relatively easy solution for giving users who don't have the add-on a reasonable experience. If you place content directly inside the OBJECT element, browsers will render that content in the case of failure. Therefore, the OBJECT element in Listing 1.1 could be updated as follows to downgrade the logo to a simple image for viewers without Silverlight:
<object type="application/x-silverlight" id="silverlightControl" width="390" height="100"> <param name="background" value="Yellow"/> <param name="source" value="Chapter1.xaml"/> <!-- Alternative content: --> <img src="logo.png"/> </object>
The logo in logo.png could look identical to the Silverlight logo shown in Figure 1.1, except that the live video would be a static image instead. If you don't want to create a downgraded version of your Silverlight content, you could always notify the user and help her install the Silverlight add-on:
<object type="application/x-silverlight" id="silverlightControl" width="390" height="100"> <param name="background" value="Yellow"/> <param name="source" value="Chapter1.xaml"/> <!-- Alternative content: --> This content requires Silverlight. <a href= "http://www.microsoft.com/silverlight/downloads.aspx">Get it here.</a> </object>
Unfortunately, Apple's Safari web browser doesn't currently support the OBJECT element. Instead, you must use an element called EMBED, which also happens to work in Internet Explorer and Firefox. Listing 1.3 contains this update to Listing 1.1 in order to work on Safari as well.
Listing 1.3. Embedding Silverlight Content Using EMBED Instead of OBJECT
<html> <head> <title>Great Estates</title> </head> <body style="background:blue"> <!-- A Silverlight-based logo: --> <embed type="application/x-silverlight" id="silverlightControl" width="390" height="100" background="Yellow" source="Chapter1.xaml"/> <p style="font-family:Tahoma; color:white"> An idyllic new community located high on a hill and offering captivating waterfront views. Tailored to meet both the needs of upsizing and downsizing buyers, Great Estates offers custom quality architecture and design at an affordable price point. </p> </body> </html>
Besides the different element name (EMBED versus OBJECT), the only other difference is that the custom parameters are specified as attributes of the EMBED element rather than as child elements. Alternative content (for when the embedding fails) can be specified with a separate NOEMBED element. The result from using EMBED looks the same as Figure 1.1 (at least the Silverlight content), as seen in Figure 1.3.
Figure 1.3 Silverlight content manually hosted in a web page with the EMBED element, viewed in Apple's Safari browser on Mac OS X.
Using EMBED is the simplest way to get your content rendered in all supported browsers, despite the fact that OBJECT is preferred for Internet Explorer and Firefox.