March 2008 - Important Note
This tutorial is no longer valid. It was written for Microsoft Silverlight 1.1 Alpha Refresh, which no longer exists - it has been replaced by Silverlight 2. We have left the page up for historical purposes (and because there are incoming links). Once again, the information below is invalid and in some cases misleading for Silverlight 2.
Since we've started posting Silverlight tutorials, I've had to figure out how to get Silverlight applications embedded into our blog. Needless to say, I found the process a lot harder than it should be. Visual Studio provides a test page as part of a default Silverlight project, but what if you want to take the Silverlight application and put it in a blog post like we do? This tutorial will go through all code changes, files needed, and new code required to easily embed your Silverlight apps into existing webpages.
If you're new to Silverlight, I would strongly recommend reading our earlier tutorial, Getting Started With Silverlight. All of the examples in this tutorial were created using Visual Studio 2008 beta 2 and Silverlight 1.1 Alpha Refresh.
Required Files
Let's start by figuring out what files are needed for a Silverlight application. When you create a new Silverlight project with Visual Studio, it creates all sorts of files - most of which aren't needed to display Silverlight. At a minimum, the only two files you'll need are the XAML file and your code behind file. I use C# for my code behind so I'll have a DLL. If you used Javascript, you'll have a JS file. When you build the Silverlight project, Visual Studio is going to put the the code behind in a folder named "ClientBin" inside your project directory.
The Javascript
Javascript makes up the bulk of getting Silverlight into a webpage. You'll notice that Visual Studio also outputs a file named "Silverlight.js". You only need one of these files for each webpage - even if you have multiple Silverlight apps on a single page. You'll want to put this in a script tag somewhere in your webpage - preferably up in the head. If you don't have access to the head tag - like in a blog post - just stick the script tag at the top of the post.
The other Javascript file Visual Studio makes for you is the one for the test page. We don't need everything in there, so let's just strip out the function that matters.
{
Silverlight.createObjectEx({
source: "Page.xaml",
parentElement: document.getElementById("SilverlightControlHost"),
id: "SilverlightControl",
properties: {
width: "100%",
height: "100%",
version: "1.1",
enableHtmlAccess: "true" },
events: {} });
}
This function will only work for one Silverlight control per page, so let's modify it to work for multiple applications.
{
Silverlight.createObjectEx({
source: source,
parentElement: document.getElementById(parent),
id: id,
properties: {
width: "100%",
height: "100%",
version: "1.1",
enableHtmlAccess: "true" },
events: {} });
}
Now the source, parentElement, and id are passed in as parameters instead of being hard coded inside the function.
Modify the XAML
Depending on where you're putting your files, you might have to modify the XAML file to point to the correct location of the code behind file. The default page.xaml file that Visual Studio generates will look something similar to this.
xmlns="http://schemas.microsoft.com/client/2007"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Loaded="Page_Loaded"
x:Class="EmbedSilverlight.Page;assembly=ClientBin/EmbedSilverlight.dll"
Width="640"
Height="480"
Background="White">
</Canvas>
The part of this that probably needs modified is x:Class. The assembly portion of x:Class will need to changed to point to wherever you put the code behind on your webserver. Remember, this path is relative to the location of the parent webpage and not the location of the XAML file.
The HTML
We've got the necessary files, the Javascript, and we've made the change to our XAML file. We are now ready to create some HTML code to actually display our Silverlight applications. I'm going to reuse a small Silverlight app that was created for an earlier tutorial.
In order to embed Silverlight into a website, we first need a parent element to stick it in - a div. Each parent element will need a unique id.
<script type="text/javascript">
createSilverlight("/pathToSilverlight/Page.xaml", "Example1Host", "Example1");
</script>
</div>
Here's the output of the above HTML code using my example application.
Edit: Example Obsoleted And Removed
So what if you want two of these? All you need to do is duplicate the above code, but with a different id.
<script type="text/javascript">
createSilverlight("/pathToSilverlight/Page.xaml", "Example1Host", "Example1");
</script>
</div>
<div id="Example2Host" style="width:120px;height:44px;">
<script type="text/javascript">
createSilverlight("/pathToSilverlight/Page.xaml", "Example2Host", "Example2");
</script>
</div>
Edit: Example Obsoleted And Removed
Hopefully this tutorial helps some of you out there trying to embed Silverlight applications in your websites or blogs. If you have any problems with the above code or any other questions, leave a comment.
09/17/2007 - 19:38
I have Silverlight 1.0 RTM installed here... your examples show up as "Get Microsoft Silverlight" links with "Alpha" in the corner, suggesting you're doing this with v1.1? It'd be nice if embedding Silverlight could be proven using RTM..
01/12/2008 - 06:29
Thank you this will help me put other silverlight controls on my main page. I apreciate what you did.
I tried it and it works perfectly
01/13/2008 - 07:23
Is it possible to implant a web page inside the silverlight module?
I mean that the main application is silverlight and hiiting on some buttons that remain still will show in an inner frame a web page
Thanks Yulia
03/12/2008 - 08:54
Very nice site!
04/04/2008 - 01:40
how to plug in two different silverlight in one webpage? need two createSilverlight.js?
04/04/2008 - 06:25
The very last example in the tutorial demonstrates how to add multiple Silverlight applications to the same page.
<script type="text/javascript">
createSilverlight("/pathToSilverlight/Page.xaml",
"Example1Host", "Example1");
</script>
</div>
<div id="Example2Host" style="width:120px;height:44px;">
<script type="text/javascript">
createSilverlight("/pathToSilverlight/Page.xaml",
"Example2Host", "Example2");
</script>
</div>
I happened to use the same application, but you can use different ones if you want. You just have to give the createSilverlight function a different XAML file. You can use the same .js file for all of them. I had to modify the script a little (as seen in the tutorial) to make this easier.
04/04/2008 - 11:14
As noted at the top, this tutorial is no longer valid and only listed here for historical purposes. Because of this, comments are now closed for this post.