Export (0) Print
Expand All

Application.Startup Event

Occurs when the Run method of the Application object is called.

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

public event StartupEventHandler Startup
<object Startup="StartupEventHandler" .../>

A typical Windows Presentation Foundation (WPF) 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.

You can declaratively specify the main window and application-scope resources using XAML (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. Programmatic initialization can be performed by handling the Startup event, including the following:

  • 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.

  • Initialize application-scope resources by using the Resources property.

  • Initialize application-scope properties by using the Properties property.

  • Instantiate and show one (or more) windows.

NoteNote

Command-line parameters can also be acquired by calling the static GetCommandLineArgs method of the Environment object. However, GetCommandLineArgs requires full trust to execute.

If you set StartupUri using XAML, 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. If you need access to the main window during startup, you need to manually create a new window object from your Startup event handler.

NoteNote

If your application uses CredentialPolicy to specify a credential policy, you need to set CredentialPolicy after Startup is raised; otherwise, WPF sets it to a default internal policy directly after the Startup event has been raised.

The command-line arguments that are passed to the Startup event handler are not the same as the URL query string parameters that are passed to an XAML browser application (XBAP).

The following example shows how to acquire and process command-line options from a standalone application. 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; // Application, StartupEventArgs, WindowState 

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();
        }
    }
}

XAML browser applications (XBAPs) cannot retrieve and process command-line arguments because they are launched with ClickOnce deployment (see Deploying a WPF Application (WPF)). However, they can retrieve and process query string parameters from the URLs that are used to launch them.

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

Show:
© 2014 Microsoft