288Microsoft Visual Studio 2010: A Beginner’s Guide

Similar to WPF applications, Silverlight applications start with a MainPage.xaml file and an App.xaml file, where App.xaml runs to initialize the application and MainPage

.xaml contains the display page. The Web site is a typical ASP.NET MVC application, except that it does have a test page that hosts the Silverlight application, SilverlightDemo CSTestPage.aspx (SilverlightDemoVBTestPage.aspx for VB). There’s also a Silverlight DemoCSTestPage.html (SilverlightDemoVBTestPage.html for VB), which performs the same function as the SilverlightDemoCSTestPage.aspx (SilverlightDemoVBTestPage

.aspx for VB) hosting Silverlight, except that the *.html version uses JavaScript and the HTML object tag to host Silverlight. Listing 10-1 shows the contents of the test page and how it hosts the Silverlight application. There is no C# or VB version of Listing 10-1 because the code is XAML, which works exactly the same with either language.

Listing 10-1 Hosting a Silverlight application on a Web page

<%@ Page Language="C#" AutoEventWireup="true" %>

<!DOCTYPE html PUBLIC

"-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server"> <title>SilverlightDemoCS</title> <style type="text/css">

//css styles omitted </style>

<script type="text/javascript" src="Silverlight.js"></script> <script type="text/javascript">

function onSilverlightError(sender, args) {

//error handling code omitted

}

</script>

</head>

<body>

<form id="form1" runat="server" style="height:100%"> <div id="silverlightControlHost">

<object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="100%" height="100%">

<param name="source" value="ClientBin/SilverlightDemoCS.xap"/>

<param name="onError" value="onSilverlightError" /> <param name="background" value="white" />

Page 311
Image 311
Microsoft 9GD00001 manual Listing 10-1 Hosting a Silverlight application on a Web