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 static property of the Application class. For example, the code in Example 2-1 is equivalent to the code in Example 2-2.

Example 2-2. 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();

Windowl window = new Windowl(); window.Show( );

Application.Current.Run(); // same as app.Run()

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

t 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).

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