Application.Startup Application.Startup Application.Startup Application.Startup Event

定义

在调用 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 

示例

下面的示例演示如何获取和处理从独立的应用程序的命令行选项。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 Microsoft.VisualBasic
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.

注解

典型的 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:

  • 获取和处理命令行参数,可从Args的属性StartupEventArgs类,该类传递到Startup事件处理程序。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.

备注

命令行参数还可以获取通过调用静态GetCommandLineArgs方法的Environment对象。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.

如果您设置StartupUri使用XAMLXAML,创建主窗口中不可用或者MainWindow属性或Windows属性Application后的对象,直到Startup处理事件。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.

备注

如果应用程序使用CredentialPolicy若要指定凭据策略,您需要设置CredentialPolicyStartup引发; 否则为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事件处理程序将不与相同URLURL查询字符串参数传递给XAML 浏览器应用程序 (XBAP)XAML browser application (XBAP)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).

适用于

另请参阅