BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

Windows Presentation Foundation
.NET 4.0+

WPF Start-up and Shutdown

The final part of the Windows Presentation Foundation Fundamentals tutorial looks at the start-up and shutdown options for WPF applications, including changing the way that a program launches and exits and detecting key events.

Start-Up

Throughout the WPF tutorial we have created examples using the window that is automatically generated when you create a new project WPF in Visual Studio. This window, which is named, "MainWindow", is launched automatically when you compile and execute your program.

Depending upon the application that you wish to develop, this might not be the desired behaviour. For example, you may wish to launch one of several windows, depending upon configuration options or the technical capabilities of the device that the software is running on. You might even wish not to open a window at all; some background applications reside in the system tray and only show windows when the user requests them.

StartupUri Property

For a simple project that opens a window automatically, the window to show is held in an application-level property called StartupUri. This is set to a Uri that defines the object to display. The property is defined against the Application object that is created when a WPF application launches. You can easily modify it in the App.xaml file.

For a standalone application, the StartupUri must identify either a Window or a NavigationWindow. Both are a form of window but are used for different styles of application. If you are hosting your WPF software in a web browser, there are a few other options for a start-up object. NB: browser-hosted WPF applications and NavigationWindows are beyond the scope of this article.

Let's take a look at StartupUri. Create a new WPF application in Visual Studio named, "StartupShutdownDemo". Once the project is prepared, double-click the App.xaml file in the Solution Explorer pane. The XAML element of the file should be opened:

<Application x:Class="StartupShutdownDemo.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
         
    </Application.Resources>
</Application>

The fourth line of the XAML defines the start-up object. Note that it describes a URI that points to the MainWindow's XAML file. If you have multiple windows in your application, or you change the name of the main window, the property should be updated here.

Startup Event

If you want more control over the start-up processes, you can remove the StartupUri setting altogether. An application that has no initial window will not open a window automatically. To demonstrate, remove the StartupUri property value from the App.xaml file:

<Application x:Class="StartupShutdownDemo.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Application.Resources>
         
    </Application.Resources>
</Application>

Start the programming with debugging by pressing F5 or choosing the "Start Debugging" option from the "Debug" menu or a toolbar. You will see that Visual Studio switches to debug mode but no window appears. Stop the debugging to continue editing the code.

Once you have removed the StartupUri property, you can control the initialisation process of your software by attaching to the Startup event. To do so, register it within the App.xaml, as you might register an event against any WPF control:

<Application x:Class="StartupShutdownDemo.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             Startup="Application_Startup">
    <Application.Resources>
         
    </Application.Resources>
</Application>

Now switch to the code behind the XAML, in the App.xaml.cs file. To demonstrate that we have control over the start-up process, we'll open two instances of the main window instead of one. Add the following method to handle the event:

private void Application_Startup(object sender, StartupEventArgs e)
{
    new MainWindow().Show();
    new MainWindow().Show();
}

Run the program to see the results. You should see two windows. Close the windows to exit the program.

18 July 2015