
The Role of the Application Class
Silverlight 2.0 projects will always contain a class which extends System.Windows.Application. This class represents
a running instance of a Silverlight plug-in. It encapsulates a number of core services such as handling messages,
trapping unhandled exceptions, defining common application data, and more.
Understand that the Application type does not represent any sort of UI, but is an abstraction of a running Silverlight
plug-in. By convention, this class is named App, and is represented via the App.xaml and App.xaml.cs / App.xaml.vb
files.
The Application class defines a number of key services. Here is a partial list of important members:
.
The XAML description of your application object is quite sparse upon start-up. Here you will see the x:Class
XAML token, which is set to the name of the related class file. As well, you will find an empty resource container.
You’ll learn about Silverlight resource management later in this class.
<Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="SimpleSilverlightApplication.App">
<Application.Resources>
</Application.Resources>
</Application>
The constructor of the related code file will handle the Startup, Exit, and UnhandledException events. As well, the
constructor makes a call to InitializeComponent(). As illustrated in your next lab, the compiler will generate a partial
class file which contains the InitializeComponent() method. Finally, notice that the RootVisual property is set to a
new Page object, which is the name of the UserControl derived class.
// C# (VB code would be similar)
public partial class App : Application
{
public App()
{
// VB events would be hooked-up via the Handles keyword.
this.Startup += this.Application_Startup;
this.Exit += this.Application_Exit;
this.UnhandledException += this.Application_UnhandledException;
InitializeComponent();
}
private void Application_Startup(object sender, StartupEventArgs e)
{
// Set the UserControl to display upon startup.
this.RootVisual = new Page();
// Preform additional start up logic here.
}
private void Application_Exit(object sender, EventArgs e)
{
// Preform any clean up here.
}
private void Application_UnhandledException(object sender,
ApplicationUnhandledExceptionEventArgs e)
{
// Handle any exceptions here.
}
}
The Role of the Application Class
Table of Contents
Copyright (c) 2008. Intertech, Inc. All Rights Reserved. This information is to be used exclusively as an
online learning aid. Any attempts to copy, reproduce, or use for training is strictly prohibited.
Courseware
Training Resources
Tutorials
Application Property
|
Meaning in Life
|
Current
|
This static property provides access to the global application object.
This allows a Silverlight control to access the application, which is very helpful in that the app object tends to define core functionality for all owned objects (resources, and so on).
|
Host
|
Gets information about the host of this Silverlight plug-in.
|
Resources
|
Allows you to package up application level resources used within the Silverlight application.
|
RootVisual
|
Specifies the UserControl which will be displayed when the Silverlight plug-in is loaded by the browser.
|
Startup Exit
|
These are two key events that almost all Silverlight applications will handle.
|
UnhandledException
|
This event fires when an unhandled exception is encountered. This is your last chance to handle the error before reported to the hosting browser.
|
|
Services