The Role of the Application Class

Regardless of which flavor of WPF application you create, they will all make use of the System.Windows.
Application type. This class represents a global running instance of a WPF application. It encapsulates a
number of core services such as handling messages, trapping unhandled exceptions, defining common
application data, and more.

A given WPF application will only have a single Application object. Unlike Windows Forms, the WPF
Application class exposes most services as instance-level members. Therefore, you will need to create a class
extending Application. This class is commonly termed the
application object.

The Application class defines a number of key services. Here is a partial list of important members:












































The application object will define the
Main() method of your program, which must be marked with the
[STAThread] attribute. This attribute ensures that any legacy COM objects, ActiveX controls, and the
WPF controls load into a thread-safe environment. If you do not mark your Main() method with the
[STAThread] attribute, you will receive a runtime exception.

Here is a simple WPF application object:


// Define the global application object
// for this WPF program.
// C#

class MyApp : Application
{
 
[STAThread]
 static void Main()
 {
   // Create an instance of MyApp.
   // Handle events, run the application,
   // launch the main window, etc.
 }
}



' Define the global application object
' for this WPF program.
' VB

Class MyApp
 Inherits Application

 <STAThread> _
 Shared Sub Main()
   ' Create an instance of MyApp.
   ' Handle events, run the application,
   ' launch the main window, etc.
 End Sub

End Class



Your Main() method will be the place where you typically want to handle the Startup, Exit, and
DispatcherUnhandledException events. Also, your application object can define any number of application-
wide data points. You will build a more complete application object later in this chapter. For now, examine
the role of the
Window class.
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 WPF application
object.  

This allows any window (or page) 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).

MainWindow

Allows you to get or set the main window of the WPF application.

StartupUri

Typically used within a XAML description to define the resource
containing the definition of the main window.

Properties

Allows you to define application-wide variables using name / value
pairs.

Similar in concept to a ‘session variable’ in a web app in that any
part of your WPF program has access to this data.

Windows

Provides access to each Window owned by the current WPF
application which makes it simple to iterate over open windows to
change their states.

Startup
Exit

These are two key events that almost all WPF application objects
will handle.


DispatcherUnhandledException

This event fires when a WPF throws an unhandled exception. This is
your last chance to handle the error before the user is presented with
a Windows error dialog box.
Services