Application Class (System.Windows)

Switch View :
ScriptFree
.NET Framework Class Library
Application Class

Encapsulates a Windows Presentation Foundation (WPF) application.

Inheritance Hierarchy

System.Object
  System.Windows.Threading.DispatcherObject
    System.Windows.Application

Namespace:  System.Windows
Assembly:  PresentationFramework (in PresentationFramework.dll)
XMLNS for XAML: http://schemas.microsoft.com/winfx/2006/xaml/presentation, http://schemas.microsoft.com/netfx/2007/xaml/presentation
Syntax

Visual Basic
Public Class Application _
	Inherits DispatcherObject _
	Implements IQueryAmbient
C#
public class Application : DispatcherObject, 
	IQueryAmbient
Visual C++
public ref class Application : public DispatcherObject, 
	IQueryAmbient
F#
type Application =  
    class
        inherit DispatcherObject
        interface IQueryAmbient
    end
XAML Object Element Usage
<Application .../>

The Application type exposes the following members.

Constructors

  Name Description
Public method Application Initializes a new instance of the Application class.
Top
Properties

  Name Description
Public property Static member Current Gets the Application object for the current AppDomain.
Public property Dispatcher Gets the Dispatcher this DispatcherObject is associated with. (Inherited from DispatcherObject.)
Public property MainWindow Gets or sets the main window of the application.
Public property Properties Gets a collection of application-scope properties.
Public property Static member ResourceAssembly Gets or sets the Assembly that provides the pack uniform resource identifiers (URIs) for resources in a WPF application.
Public property Resources Gets or sets a collection of application-scope resources, such as styles and brushes.
Public property ShutdownMode Gets or sets the condition that causes the Shutdown method to be called.
Public property StartupUri Gets or sets a UI that is automatically shown when an application starts.
Public property Windows Gets the instantiated windows in an application.
Top
Methods

  Name Description
Public method CheckAccess Determines whether the calling thread has access to this DispatcherObject. (Inherited from DispatcherObject.)
Public method Equals(Object) Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected method Finalize Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public method FindResource Searches for a user interface (UI) resource, such as a Style or Brush, with the specified key, and throws an exception if the requested resource is not found (see Resources Overview).
Public method Static member GetContentStream Returns a resource stream for a content data file that is located at the specified Uri (see WPF Application Resource, Content, and Data Files).
Public method Static member GetCookie Retrieves a cookie for the location specified by a Uri.
Public method GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
Public method Static member GetRemoteStream Returns a resource stream for a site-of-origin data file that is located at the specified Uri (see WPF Application Resource, Content, and Data Files).
Public method Static member GetResourceStream Returns a resource stream for a resource data file that is located at the specified Uri (see WPF Application Resource, Content, and Data Files).
Public method GetType Gets the Type of the current instance. (Inherited from Object.)
Public method Static member LoadComponent(Uri) Loads a XAML file that is located at the specified uniform resource identifier (URI), and converts it to an instance of the object that is specified by the root element of the XAML file.
Public method Static member LoadComponent(Object, Uri) Loads a XAML file that is located at the specified uniform resource identifier (URI) and converts it to an instance of the object that is specified by the root element of the XAML file.
Protected method MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Protected method OnActivated Raises the Activated event.
Protected method OnDeactivated Raises the Deactivated event.
Protected method OnExit Raises the Exit event.
Protected method OnFragmentNavigation Raises the FragmentNavigation event.
Protected method OnLoadCompleted Raises the LoadCompleted event.
Protected method OnNavigated Raises the Navigated event.
Protected method OnNavigating Raises the Navigating event.
Protected method OnNavigationFailed Raises the NavigationFailed event.
Protected method OnNavigationProgress Raises the NavigationProgress event.
Protected method OnNavigationStopped Raises the NavigationStopped event.
Protected method OnSessionEnding Raises the SessionEnding event.
Protected method OnStartup Raises the Startup event.
Public method Run() Starts a Windows Presentation Foundation (WPF) application.
Public method Run(Window) Starts a Windows Presentation Foundation (WPF) application and opens the specified window.
Public method Static member SetCookie Creates a cookie for the location specified by a Uri.
Public method Shutdown() Shuts down an application.
Public method Shutdown(Int32) Shuts down an application that returns the specified exit code to the operating system.
Public method ToString Returns a string that represents the current object. (Inherited from Object.)
Public method TryFindResource Searches for the specified resource.
Public method VerifyAccess Enforces that the calling thread has access to this DispatcherObject. (Inherited from DispatcherObject.)
Top
Events

  Name Description
Public event Activated Occurs when an application becomes the foreground application.
Public event Deactivated Occurs when an application stops being the foreground application.
Public event DispatcherUnhandledException Occurs when an exception is thrown by an application but not handled.
Public event Exit Occurs just before an application shuts down, and cannot be canceled.
Public event FragmentNavigation Occurs when a navigator in the application begins navigation to a content fragment, Navigation occurs immediately if the desired fragment is in the current content, or after the source XAML content has been loaded if the desired fragment is in different content.
Public event LoadCompleted Occurs when content that was navigated to by a navigator in the application has been loaded, parsed, and has begun rendering.
Public event Navigated Occurs when the content that is being navigated to by a navigator in the application has been found, although it may not have completed loading.
Public event Navigating Occurs when a new navigation is requested by a navigator in the application.
Public event NavigationFailed Occurs when an error occurs while a navigator in the application is navigating to the requested content.
Public event NavigationProgress Occurs periodically during a download that is being managed by a navigator in the application to provide navigation progress information.
Public event NavigationStopped Occurs when the StopLoading method of a navigator in the application is called, or when a new navigation is requested by a navigator while a current navigation is in progress.
Public event SessionEnding Occurs when the user ends the Windows session by logging off or shutting down the operating system.
Public event Startup Occurs when the Run method of the Application object is called.
Top
Explicit Interface Implementations

  Name Description
Explicit interface implemetation Private method IQueryAmbient.IsAmbientPropertyAvailable Queries for whether a specified ambient property is available in the current scope.
Top
Remarks

Application is a class that encapsulates WPF application-specific functionality, including the following:

Application implements the singleton pattern to provide shared access to its window, property, and resource scope services. Consequently, only one instance of the Application class can be created per AppDomain.

You can implement an Application using markup, markup and code-behind, or code. If Application is implemented with markup, whether markup or markup and code-behind, the markup file must be configured as an Microsoft build engine (MSBuild) ApplicationDefinition item.

Note Note

A standalone application does not require an Application object; it is possible to implement a custom static entry point method (Main) that opens a window without creating an instance of Application. However, XAML browser applications (XBAPs) require an Application object.

Examples

The following example shows how a standard application is defined using only markup:

XAML

<Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" />


The following example shows how a standard application is defined using only code:

Visual Basic


Imports Microsoft.VisualBasic
Imports System ' STAThread
Imports System.Windows ' Application

Namespace SDKSample
	Public Class AppCode
		Inherits Application
		' Entry point method
		<STAThread>
		Public Shared Sub Main()
			Dim app As New AppCode()
			app.Run()
		End Sub
	End Class
End Namespace


C#

using System; // STAThread
using System.Windows; // Application

namespace SDKSample
{
    public class AppCode : Application
    {
        // Entry point method
        [STAThread]
        public static void Main()
        {
            AppCode app = new AppCode();
            app.Run();
        }
    }
}


The following example shows how a standard application is defined using a combination of markup and code-behind.

XAML

<Application 
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
  x:Class="SDKSample.App" />


Visual Basic


Imports Microsoft.VisualBasic
Imports System.Windows ' Application

Namespace SDKSample
	Partial Public Class App
		Inherits Application
	End Class
End Namespace


C#

using System.Windows;  // Application

namespace SDKSample
{
    public partial class App : Application { }
}


Version Information

.NET Framework

Supported in: 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1
Platforms

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Thread Safety

The public static (Shared in Visual Basic) members of this type are thread safe. In addition, the FindResource and TryFindResource methods and the Properties and Resources properties are thread safe.

See Also

Reference

Other Resources