Assembly: PresentationFramework (in PresentationFramework.dll)
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 event, including the following:
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.
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 event is processed. If you need access to the main window during startup, you need to manually create a new window object from your event handler.
If your application uses CredentialPolicy to specify a credential policy, you need to set CredentialPolicy after is raised; otherwise, WPF sets it to a default internal policy directly after the event has been raised.
The command-line arguments that are passed to theevent 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" />
Imports Microsoft.VisualBasic Imports System.Windows ' Application, StartupEventArgs, WindowState 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 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.
Available since 3.0
Available since 2.0
Windows Phone Silverlight
Available since 7.0