Translate

Saturday, 3 August 2013

WPF Applications and Settings

WPF Application Lifetime


In the Windows sense, an “application” is an address space and at least one thread of
execution (a.k.a. a “process”). In the WPF sense, an application is a singleton object
that provides services for UI components and UI programmers in the creation and
execution of a WPF program. More specifically, in WPF, an application is an
instance of the Application class from the System.Windows namespace.

WPF Explicit Application Creation

Example . Creating an application explicitly
using System;
using System.Windows; // the home of the Application class
class Program {
[STAThread]
static void Main( ) {
Application app = new System.Windows.Application();
Window1 window = new Window1( );
window.Show( );
app.Run();
}
}

Here, we’re creating an application inside an STA thread,* creating a window and
showing it, and then running the application. While the application is running, WPF
processes Windows messages and routes events to WPF UI objects as necessary.
When the Run method returns, messages have stopped being routed and generally
don’t start again (unless you show a modal window after the Run method returns, but
that’s not something you’ll usually do). During its lifetime, the application provides
various services.

WPF Application Access

One of the services the Application class provides is access to the current instance.
Once an instance of the Application class is created,† it’s available via the Current
staticproperty of the Application class. For example, the code in Example 2-1 is
equivalent to the code in Example 
Here, in the process’s entry point, we’re creating an application, creating and showing
the main window, and then running the application. Creation of the Application
object fills the static Application.Current property. Access to the current application
is very handy in other parts of your program where you don’t create the application
or when you let WPF create the application for you itself.
* The “Single Threaded Apartment” (STA) was invented as part of the native Component Object Model
(COM) to govern the serialization of incoming COM calls. All Microsoft presentation frameworks, native or
managed, require that they be run on a thread initialized as an STA thread so that they can integrate with
one another and with COM services (e.g., drag-and-drop).
† WPF makes sure that, at most, one Application object is created per application domain. For a discussion
of .NET application domains, I recommend Essential .NET, by Don Box with Chris Sells (Addison-Wesley
Professional)

Example . Implicitly filling in the Application.Current property
using System;
using System.Windows;
class Program {
[STAThread]
static void Main( ) {
// Fills in Application.Current
Application app = new System.Windows.Application();
Window1 window = new Window1( );
window.Show( );
Application.Current.Run(); // same as app.Run( )
}
}


Implicit Application Creation

Because a Main method that creates and runs an application is pretty darn common,
WPF can provide the process’s entry point for you. WPF projects generally designate
one XAML file that defines the application. For example, if we had defined our
application in a XAML file with code behind, it would look like Example 2-3.
Notice that Example 2-3 is defining a custom application class in this code
(ImplicitAppSample.App) that derives from the Application class. In the OnStartup
override, we’re only creating a window and showing it, assuming WPF is going to
create the Main for us that creates the instance of the App class and calls the Run
method (which calls the OnStartup method). The way that WPF knows which XAML
file contains the definition of the Application class is that the Build Action is set to
ApplicationDefinition.

The ApplicationDefinition Build Action lets WPF know which class is our application
and hooks it up appropriately in a Main method it generates for us, which saves
us from writing several lines of boilerplate code.
Example  Declaring an application in XAML
<!-- App.xaml -->
<Application
x:Class="ImplicitAppSample.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" />
// App.xaml.cs
using System;
using System.Windows;
namespace ImplicitAppSample {
public partial class App : System.Windows.Application {
protected override void OnStartup(StartupEventArgs e) {
// let the base class have a crack
base.OnStartup(e);
// WPF itself is providing the Main that creates an
// Application and calls the Run method; all we have
// to do is create a window and show it
Window1 window = new Window1( );
window.Show( );
}
}
}

No comments: