Silverlight 2 Application Structure and Execution Cycle

Posted by: Suprotim Agarwal , on 11/6/2008, in Category Silverlight 2, 3, 4 and 5
Views: 34660
Abstract: In this article we will take a quick overview of the main components that play a role in the Execution cycle of a Silverlight 2 application. We also see the important steps in a Silverlight application lifecycle.
Silverlight 2 Application Structure and Execution Cycle
In this article we will take a quick overview of the main components that play a role in the Execution cycle of a Silverlight 2 application. We also see the important steps in a Silverlight application lifecycle. I have assumed that you have created a Silverlight application using Visual Studio 2008 and have compiled it. For understanding purposes, let us have a look at the screenshot below and understand the different components.
If you observe, there is an HTML and an .aspx page in the project. Let us first explore the contents of these two pages:
AppLifeCycleTestPage.html – The HTML page contains an object tag which looks similar to the following:
<object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="100%" height="100%">
      <param name="source" value="ClientBin/AppLifeCycle.xap"/>
      <param name="onerror" value="onSilverlightError" />
      <param name="background" value="white" />
      <param name="minRuntimeVersion" value="2.0.31005.0" />
      <param name="autoUpgrade" value="true" />
      <a href="" style="text-decoration: none;">
            <img src="" alt="Get Microsoft Silverlight" style="border-style: none"/>
The ‘data’ attribute in the <object> tag represents the Silverlight browser plug-in that will be instantiated. The data:application/x-silverlight-2 is the MIME type for Silverlight 2 (final). The ‘type’ attribute represents the Silverlight 2 application. The source of the application is represented using the <param name="source" . If you do not have Silverlight plug-in installed, this tag detects it and forwards you to the URL represented using the anchor tag <a href="" style="text-decoration: none;">
Note 1: The <object> tag is capable of instantiating the plug-in directly by using the <param> children to set the parameters. If you want control over the object creation and set some properties before the load, you can use the Silverlight.js file which we will view shortly.
Note 2: The observant ones might have noticed a commented line just below the <html> element
<!-- saved from url=(0014)about:internet -->
This feature is called Mark Of The Web(MOTW).
AppLifeCycleTestPage.aspx – The .aspx page contains the same functionality as in the .html page, wrapped up in the <asp:Silverlight control. This control finally renders the the <object> tag we saw earlier.
<asp:Silverlight ID="Xaml1" runat="server" Source="~/ClientBin/AppLifeCycle.xap" MinimumVersion="2.0.31005.0" Width="100%" Height="100%" />
Silverlight.js – If you observe the <head> tag in the .html page, you will see a reference to the Silverlight.js file
    <script type="text/javascript" src="Silverlight.js"></script>
This JavaScript file contains methods called ‘createObject’ and ‘createObjectEx’ used to instantiate Silverlight.
AppLifeCycle.xap - Exploring the Application package (.xap)
When you build a Silverlight managed application, the build process generates an application package in the ClientBin folder. The managed API packages assemblies and resources into this application package. An application package has a .xap file extension which is a zip file. You can view the contents of this package by renaming it as .zip and extracting the contents as shown below
The .xap contains the files required to initiate and execute your application. The application package also includes a manifest file (AppManifest.xaml) which identifies packaged assemblies as well as external library assemblies that can be downloaded on demand after application startup.
<Deployment xmlns="" xmlns:x="" EntryPointAssembly="AppLifeCycle" EntryPointType="AppLifeCycle.App" RuntimeVersion="2.0.31005.0">
    <AssemblyPart x:Name="AppLifeCycle" Source="AppLifeCycle.dll" />
Note how the EntryPointAssembly and the Deployment.Parts refer to the AppLifeCycle.dll that contains our MSIL code. The EntryPointType specifies the class contained within the assembly (DLL), which is the Start point of the application. In our application, this class is the AppLifeCycle.App (App.Xaml.cs or App.Xaml.vb)
The Application package can also include resource files, such as images or videos or fonts.
Note: If you have to run a Silverlight application on a server not supporting ASP.NET, just deploy the .xap and the silverlight.js file along with the HTML page.
Note: The .xap must include an application assembly containing your Application class, in our case, the class referred to in the EntryPointType attribute.
What purpose does this Application class serve?
This Application class encapsulates the interaction between the Silverlight application and the Silverlight plug-in. Other than providing an Application start point, it manages resources as well as provides events useful during the lifecycle of the Silverlight application. We will see some of these events in just a moment.
Combining Silverlight pieces together
We have seen the main components that are involved in the execution cycle of a Silverlight application. Let us see how they work together and how do they get called.
When you embed the Silverlight plug-in in a Web page, you specify the location of the application package that the plug-in should download, which we have discussed earlier. The process is as follows:
1. A User requests a Web page that hosts the Silverlight plug-in
2. If the correct version of Silverlight has been installed, the Web browser loads the Silverlight plug-in and creates the Silverlight content region. The plug-in then begins downloading your .xap file.
3. The plug-in extract the contents of the .xap file and reads the AppManifest.xaml file from the XAP to determine the assemblies your application uses.
4. The plug-in loads the Silverlight core services and the Silverlight runtime environment (CLR) which then creates an Application Domain for your application.
5. When the package is downloaded, the plug-in then loads your application assemblies and dependencies (if any) into the AppDomain. The CLR uses the metadata in .xap to instantiate your Application class and the default constructor of the Application class raises its Startup event and other events as shown below
public App()
    this.Startup += this.Application_Startup;
    this.Exit += this.Application_Exit;
    this.UnhandledException += this.Application_UnhandledException;
private void Application_Startup(object sender, StartupEventArgs e)
    this.RootVisual = new Page();
private void Application_Exit(object sender, EventArgs e)
private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
    Public Sub New()
    End Sub
    Private Sub Application_Startup(ByVal o As Object, ByVal e As StartupEventArgs) Handles Me.Startup
        Me.RootVisual = New Page()
    End Sub
    Private Sub Application_Exit(ByVal o As Object, ByVal e As EventArgs) Handles Me.Exit
    End Sub
    Private Sub Application_UnhandledException(ByVal sender As Object, ByVal e As ApplicationUnhandledExceptionEventArgs) Handles Me.UnhandledException
    End Sub
The Startup event handler is handled to display a user interface page and that is how you view your UI. This event occurs once the Silverlight plug-in has finished loading the .xap file. Once the UI is loaded, other events occur in response to user actions.
The Exit event handler can be used to process some code just before the application is released from the memory. The UnhandledException handler enables you to handle errors that are not anticipated in advance.
Well that was a quick overview of the different bits of a Silverlight application. I hope this article was useful and I thank you for viewing it.
If you liked the article,  Subscribe to my RSS Feed.

This article has been editorially reviewed by Suprotim Agarwal.

Absolutely Awesome Book on C# and .NET

C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.

We at DotNetCurry are very excited to announce The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).

Organized around concepts, this Book aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on the latest .NET Core 3.0, .NET Standard and C# 8.0 (final release) too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.

Click here to Explore the Table of Contents or Download Sample Chapters!

What Others Are Reading!
Was this article worth reading? Share it with fellow developers too. Thanks!
Share on LinkedIn
Share on Google+

Suprotim Agarwal, MCSD, MCAD, MCDBA, MCSE, is the founder of DotNetCurry, DNC Magazine for Developers, SQLServerCurry and DevCurry. He has also authored a couple of books 51 Recipes using jQuery with ASP.NET Controls and The Absolutely Awesome jQuery CookBook.

Suprotim has received the prestigious Microsoft MVP award for Fifteen consecutive years. In a professional capacity, he is the CEO of A2Z Knowledge Visuals Pvt Ltd, a digital group that offers Digital Marketing and Branding services to businesses, both in a start-up and enterprise environment.

Get in touch with him on Twitter @suprotimagarwal or at LinkedIn

Page copy protected against web site content infringement 	by Copyscape

Feedback - Leave us some adulation, criticism and everything in between!
Comment posted by Khushboo on Tuesday, May 4, 2010 2:57 AM
Really very very helpful article for understanding Silverlight Application Life Cycle.
Thanks a Lot!!!