Window Lifetime

Before you can see a window, you need to create it and show it. Creating a window is as easy as calling new on the Window class or a class derived from Window. As you can see in Example 10-1, if you've defined a custom window in XAML—Visual Studio 2005 does this when you right-click on a project in the Solution Explorer and select Add ^ New Item and then choose Window (WPF)—the constructor will need to call the InitializeComponent method (as the Visual Studio 2005-generated Window classes already do).

Example 10-1. Calling InitializeComponent <!-- Windowl.xaml -->

<Window x:Class="WindowsApplication1.Window1" ...>

... <!-- XAML to initialize instance of custom Window class --> </Window>

// Window1.xaml.cs namespace WindowsApplicationl { public partial class Windowl : System.Windows.Window { public Windowl() { // Use XAML to initialize object of custom Window class first InitializeComponent();

In the presence of the x:Class property, the WPF build task will generate a partial class with an InitializeComponent implementation. This will load the window's XAML and use it to initialize the properties that make up the window object and hook up the events. If you forget to call InitializeComponent or do something else before calling InitializeComponent, your window object will not be fully initialized.

After the window has been constructed, you can show it modally or modelessly (see Example 10-2).

Example 10-2. Showing a window modelessly and modally Windowl window = new Windowl();

// Show modelessly window.Show();

// Show modally if( window.ShowDialog() == true ) {

// user clicked OK (or the equivalent)

When a window is shown modelessly using the Show method, the Show method returns immediately, not waiting for the newly shown window to close. This is the method to use to create a new top-level window in your application.

When a window is shown modally using the ShowDialog method, the ShowDialog method doesn't return until the newly shown window is closed, disabling the other windows in your application. The ShowDialog method is mostly used to show windows that act as dialogs, which we'll see in the "Dialogs" section, later in this chapter.

Once a window is created, it exposes a series of events to let you monitor and affect its lifetime, as shown in Figure 10-2.

Figure 10-2. Window lifetime

The following is a description of these events:


Raised when the FrameworkElement base of the Window is initialized (essentially when the InitializeComponent method is called). LocationChanged

Fired when the window is moved.


Raised when the window is activated (e.g., clicked on). If the window is never activated, you won't get this event (or the corresponding Deactivated event). Deactivated

Raised when some other window in the system is activated. Loaded

Raised just before the window is shown. ContentRendered

Raised when the window's content is visually rendered. Closing

Raised when the window attempts to close itself. You can cancel this by setting the CancelEventArgs argument's Cancel property to true.


Raised when the window has been closed (cannot be canceled). Unloaded

Raised after the window has been closed and removed from the visual tree. If closing the window causes the application to shut down, you won't see this event.

Was this article helpful?

0 0
Project Management Made Easy

Project Management Made Easy

What you need to know about… Project Management Made Easy! Project management consists of more than just a large building project and can encompass small projects as well. No matter what the size of your project, you need to have some sort of project management. How you manage your project has everything to do with its outcome.

Get My Free Ebook

Post a comment