共用方式為


Application Class

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Encapsulates a Silverlight application.

Inheritance Hierarchy

System.Object
  System.Windows.Application

Namespace:  System.Windows
Assembly:  System.Windows (in System.Windows.dll)

Syntax

'Declaration
Public Class Application
public class Application
<Application .../>

The Application type exposes the following members.

Constructors

  Name Description
Public methodSupported by Silverlight for Windows Phone Application Initializes a new instance of the Application class.

Top

Properties

  Name Description
Public propertySupported by Silverlight for Windows Phone ApplicationLifetimeObjects Gets the application extension services that have been registered for this application.
Public propertyStatic memberSupported by Silverlight for Windows Phone Current Gets the Application object for the current application.
Public property HasElevatedPermissions Gets a value that indicates whether the application is running with elevated permissions.
Public propertySupported by Silverlight for Windows Phone Host Gets various details about the Silverlight application's host.
Public property InstallState Gets the current out-of-browser installation state of the application.
Public property IsRunningOutOfBrowser Gets a value that indicates whether the application was launched from the out-of-browser state.
Public property MainWindow Gets the out-of-browser application window.
Public propertySupported by Silverlight for Windows Phone Resources Gets a collection of application-scoped resources, such as styles, templates, and brushes.
Public propertySupported by Silverlight for Windows Phone RootVisual Gets or sets the main application UI.
Public property Windows Gets a collection of the Window instances that have been created.

Top

Methods

  Name Description
Public method CheckAndDownloadUpdateAsync Launches an asynchronous process to check for and download an updated version of the application.
Public methodSupported by Silverlight for Windows Phone Equals(Object) Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodSupported by Silverlight for Windows Phone Finalize Allows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)
Public methodSupported by Silverlight for Windows Phone GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
Public methodStatic memberSupported by Silverlight for Windows Phone GetResourceStream(Uri) Returns a resource file from a location in the application package.
Public methodStatic memberSupported by Silverlight for Windows Phone GetResourceStream(StreamResourceInfo, Uri) Returns a resource file from a location in the specified zip package.
Public methodSupported by Silverlight for Windows Phone GetType Gets the Type of the current instance. (Inherited from Object.)
Public method Install Attempts to install the application so that it can run outside the browser.
Public methodStatic memberSupported by Silverlight for Windows Phone LoadComponent 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 methodSupported by Silverlight for Windows Phone MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public methodSupported by Silverlight for Windows Phone ToString Returns a string that represents the current object. (Inherited from Object.)

Top

Events

  Name Description
Public event CheckAndDownloadUpdateCompleted Occurs when the application has finished checking for updates in response to a CheckAndDownloadUpdateAsync method call.
Public eventSupported by Silverlight for Windows Phone Exit Occurs just before an application shuts down and cannot be canceled.
Public event InstallStateChanged Occurs when the InstallState property value changes.
Public eventSupported by Silverlight for Windows Phone Startup Occurs when an application is started.
Public eventSupported by Silverlight for Windows Phone UnhandledException Occurs when an exception that is raised by Silverlight is not handled.

Top

Remarks

Application is a class that encapsulates a Silverlight application and provides the following services:

  • Application Entry Point

  • Application Lifetime

  • Application Management

  • Application-Scoped Resources

  • Unhandled Exception Detection

The entry point in a Silverlight application is the class in your application assembly that derives from Application. This class is known as the application class. When the Silverlight plug-in starts, Silverlight uses metadata in the application package to instantiate the application class. At this point, the application's lifetime starts. The lifetime of an application occurs in the following order:

  • Construction (Application)

  • Initialization (Startup)

  • Application Running

  • ...

  • Application Shutting Down (initiated by user)

  • Exit (Exit)

By handling the Startup event, you can retrieve and process initialization parameters from the InitParams property of the StartupEventArgs object that is passed to the Startup event handler. Initialization parameters are configured by using the initParams property of the HTML object element that is used to configure and instantiate the Silverlight plug-in.

During startup, you can also specify the main application UI to show by setting the RootVisual property.

Once an application is running, the Application object and its state can be accessed from the static Current property. The singleton pattern ensures that state managed by Application, including shared resources (Resources) and custom properties, is available from a single, application-scoped location.

XAML Usage Notes for the Application class

In practical XAML usage, Application is almost always the root element of the App.xaml file and therefore a practical usage would include the client and XAML xmlns values, as well as an x:Class value for the code-behind that initiates the root visual, as shown in the following XAML fragment.

<Application
  xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml" 
  x:Class="namespace.class"... >
  ...
</Application>

Technically the XAML usage for any member of the Application class that supports XAML applies both to the Application class and also to any Application subclass that does not specifically disable XAML usage by shadowing members or changing the access level of its constructors. However, the XAML usage for Application members shows the literal Application rather than the placeholder application because subclassing Application is not necessary for most Silverlight application model scenarios, and the XAML usage shows the most common usage patterns whenever possible.

Examples

The following code example demonstrates how to use this class to merge the XAML and code-behind portions of a Page class. This code is similar to the InitializeComponent method that Visual Studio generates for the same purpose.

Visual Studio generates the InitializeComponent method when a XAML file has a build action of Page. To load a XAML file using the following example code, set its build action to Resource.

Private LayoutRoot As System.Windows.Controls.Grid

Public Sub New()
    System.Windows.Application.LoadComponent(Me, New System.Uri( _
        "/SilverlightApplication1;component/Page.xaml", _
        System.UriKind.Relative))
    Me.LayoutRoot = CType(Me.FindName("LayoutRoot"),  _
        System.Windows.Controls.Grid)
End Sub
private System.Windows.Controls.Grid LayoutRoot;

public Page()
{
    System.Windows.Application.LoadComponent(this, new System.Uri(
        "/SilverlightApplication1;component/Page.xaml", 
        System.UriKind.Relative));
    this.LayoutRoot = ((System.Windows.Controls.Grid)
        (this.FindName("LayoutRoot")));
}

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.