Application.Startup Event

Definition

在调用 Run() 对象的 Application 方法时发生。Occurs when the Run() method of the Application object is called.

public:
 event System::Windows::StartupEventHandler ^ Startup;
public event System.Windows.StartupEventHandler Startup;
member this.Startup : System.Windows.StartupEventHandler 
Public Custom Event Startup As StartupEventHandler 

Examples

下面的示例演示如何从独立的应用程序获取和处理命令行选项。The following example shows how to acquire and process command-line options from a standalone application. 如果提供了 /StartMinimized命令行参数,则应用程序将打开处于最小化状态的主窗口。If the /StartMinimized command-line parameter was provided, the application opens the main window in a minimized state.

<Application
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  x:Class="SDKSample.App"
  Startup="App_Startup" />
using System.Windows;

namespace SDKSample
{
    public partial class App : Application
    {
        void App_Startup(object sender, StartupEventArgs e)
        {
            // Application is running
            // Process command line args
            bool startMinimized = false;
            for (int i = 0; i != e.Args.Length; ++i)
            {
                if (e.Args[i] == "/StartMinimized")
                {
                    startMinimized = true;
                }
            }

            // Create main application window, starting minimized if specified
            MainWindow mainWindow = new MainWindow();
            if (startMinimized)
            {
                mainWindow.WindowState = WindowState.Minimized;
            }
            mainWindow.Show();
        }
    }
}

Imports System.Windows

Namespace SDKSample
    Partial Public Class App
        Inherits Application
        Private Sub App_Startup(ByVal sender As Object, ByVal e As StartupEventArgs)
            ' Application is running
            ' Process command line args
            Dim startMinimized As Boolean = False
            Dim i As Integer = 0
            Do While i <> e.Args.Length
                If e.Args(i) = "/StartMinimized" Then
                    startMinimized = True
                End If
                i += 1
            Loop

            ' Create main application window, starting minimized if specified
            Dim mainWindow As New MainWindow()
            If startMinimized Then
                mainWindow.WindowState = WindowState.Minimized
            End If
            mainWindow.Show()
        End Sub
    End Class
End Namespace

XAML 浏览器应用程序 (XBAP)XAML browser applications (XBAPs) 无法检索和处理命令行参数,因为它们是使用 ClickOnceClickOnce 部署启动的(请参阅部署 Wpf 应用程序(wpf))。cannot retrieve and process command-line arguments because they are launched with ClickOnceClickOnce deployment (see Deploying a WPF Application (WPF)). 但是,它们可以检索和处理来自用于启动它们的 URL 的查询字符串参数。However, they can retrieve and process query string parameters from the URLs that are used to launch them.

Remarks

典型的 Windows Presentation Foundation 应用程序在启动时可能会执行各种初始化任务,包括:A typical Windows Presentation Foundation application may perform a variety of initialization tasks when it starts up, including:

  • 处理命令行参数。Processing command-line parameters.

  • 打开主窗口。Opening the main window.

  • 正在初始化应用程序范围资源。Initializing application-scope resources.

  • 正在初始化应用程序范围的属性。Initializing application-scope properties.

可以使用 XAMLXAML (分别为StartupUriResources)以声明方式指定主窗口和应用程序范围的资源。You can declaratively specify the main window and application-scope resources using XAMLXAML (StartupUri and Resources, respectively). 但有时,只能在运行时以编程方式确定应用程序的资源或主窗口。Sometimes, however, the resources or main window of your application can only be determined programmatically at run time. 此外,应用程序范围的属性和命令行参数只能以编程方式使用。Additionally, application-scope properties and command-line parameters can only be used programmatically. 可以通过处理 Startup 事件来执行编程初始化,其中包括:Programmatic initialization can be performed by handling the Startup event, including the following:

  • 获取和处理命令行参数,该参数可从传递到 Startup 事件处理程序的 StartupEventArgs 类的 Args 属性中获取。Acquire and process command-line parameters, which are available from the Args property of the StartupEventArgs class that is passed to the Startup event handler.

  • 使用 Resources 属性初始化应用程序范围资源。Initialize application-scope resources by using the Resources property.

  • 使用 Properties 属性初始化应用程序范围的属性。Initialize application-scope properties by using the Properties property.

  • 实例化并显示一个(或多个)窗口。Instantiate and show one (or more) windows.

Note

还可以通过调用 Environment 对象的静态 GetCommandLineArgs 方法来获取命令行参数。Command-line parameters can also be acquired by calling the static GetCommandLineArgs method of the Environment object. GetCommandLineArgs 需要完全信任才能执行。However, GetCommandLineArgs requires full trust to execute.

如果使用 XAMLXAML设置 StartupUri,则在处理 Application 事件之前,创建的主窗口无法从 Startup 对象的 MainWindow 属性或 Windows 属性中获得。If you set StartupUri using XAMLXAML, the main window that is created is not available from either the MainWindow property or the Windows property of the Application object until after the Startup event is processed. 如果在启动期间需要访问主窗口,则需要从 Startup 事件处理程序中手动创建新的窗口对象。If you need access to the main window during startup, you need to manually create a new window object from your Startup event handler.

Note

如果你的应用程序使用 CredentialPolicy 来指定凭据策略,则需要在引发 Startup 后设置 CredentialPolicy;否则,WPFWPF 在引发 Startup 事件之后直接将它设置为默认内部策略。If your application uses CredentialPolicy to specify a credential policy, you need to set CredentialPolicy after Startup is raised; otherwise, WPFWPF sets it to a default internal policy directly after the Startup event has been raised.

传递给 Startup 事件处理程序的命令行参数与传递到 XAML 浏览器应用程序 (XBAP)XAML browser application (XBAP)URLURL 查询字符串参数不同。The command-line arguments that are passed to the Startup event handler are not the same as the URLURL query string parameters that are passed to an XAML 浏览器应用程序 (XBAP)XAML browser application (XBAP).

Applies to

See also