How do you add Silverlight to your web page? A typical Silverlight project has four files: an HTML file that hosts or displays content, an aghost.js file, a XAML file, and a JavaScript file. This document describes how to create a Silverlight project and add Silverlight content to an HTML file in three steps. This guide contains the following sections.
before you get started...Before you can create Silverlight content, you'll need the following items.
step 1: add script references to your HTML page
In this step, you add references to the
Your HTML page should now contain the following basic elements:
<!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" xml:lang="en">
<head>
<title>A Sample HTML page</title>
<script type="text/javascript" src="Silverlight.js"></script>
<script type="text/javascript" src="createSilverlight.js"></script>
</head>
<body>
</body>
</html>
step 2: create the HTML host element and initialization block
You've reached the end of step 2. Your HTML file should contain the following elements.
<!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" xml:lang="en">
<head>
<title>A Sample HTML page</title>
<script type="text/javascript" src="Silverlight.js"></script>
<script type="text/javascript" src="createSilverlight.js"></script>
</head>
<body>
<!-- Where the Silverlight control will go-->
<div id="mySilverlightControlHost">
</div>
<script type="text/javascript">
// Retrieve the div element you created in the previous step.
var parentElement =
document.getElementById("mySilverlightControlHost");
// This function creates the Silverlight control.
createMySilverlightControl();
</script>
</body>
</html>
step 3: define the creation function that initializes your control
Open the
function createMySilverlightControl()
{
Sys.Silverlight.createObject(
"myxaml.xaml", // Source property value.
parentElement, // DOM reference to hosting DIV tag.
"mySilverlightControl", // Unique control ID value.
{ // Control properties.
width:'300', // Width of rectangular region of
// control in pixels.
height:'300', // Height of rectangular region of
// control in pixels.
inplaceInstallPrompt:false, // Determines whether to display
// in-place install prompt if
// invalid version detected.
background:'#D6D6D6', // Background color of control.
isWindowless:'false', // Determines whether to display control
// in Windowless mode.
framerate:'24', // MaxFrameRate property value.
version:'0.9' // Control version to use.
},
{
onError:null, // OnError property value --
// event handler function name.
onLoad:null // OnLoad property value --
// event handler function name.
},
null); // Context value -- event handler function name.
}
This script contains several parameters you might want to customize, such as the height and width of the control (percentage sizes are allowed), the name of the XAML file that contains your Silverlight content, and a value that specifies whether the control is windowless. If you are adding multiple Silverlight controls to the same page, create a new function for each one. Make sure that each function specifies a different value for the control id ("mySilverlightControl" in this example). step 4: create your Silverlight content filesNow that your HTML file is configured, it's time to create your content.
creating multiple Silverlight controlsIf you want to create more than one Silverlight control on your page, repeat steps 2, 3, and 4 for each control.
a sample project
If you've gotten stuck or would just like to see what a simple Silverlight project
looks like, copy the following files to your machine and navigate to the what's next?In the next part, create a XAML file, you'll learn how to add content to your XAML file. Copyright © 2007 Microsoft Corporation. All rights reserved. Legal Notices. |