The Application Class


The Application Class

A Longhorn program always contains a single instance of an application object. This object derives directly or indirectly from the MSAvalon. Windows .Application class and performs the following functions:

  • Provides an entry point, encapsulation, and scope for the application

  • Allows an application to share code and state across the pages that make up the application

  • Provides application-level events

  • Maintains a collection of the application s windows

  • Provides a security model

  • Defines any resources that the application uses

The MSAvalon.Windows.Application class provides basic application support to an application. You typically use it when your application needs low overhead and does not use page navigation features. However, most Longhorn platform applications use the closely related MSAvalon.Windows.NavigationApplication class, which inherits from MSAvalon.Windows.Application and adds support for navigation. I ll discuss the NavigationApplication class in detail in the following section. You will typically define a class that inherits the appropriate base class, overrides base class methods as necessary, and then registers for events to provide custom startup or shutdown procedures.

The SimpleApplication1.cs source file listing, shown here, demonstrates using the Application object. The EntryClass.Main method sets the threading state to ApartmentState.STA, [1] creates my specialized application object, MyApp , and calls its Run method to launch the application. The MyApp class overrides the OnStartingUp method, which receives control when the application is starting up. When the system invokes the OnStartingUp method, I call a helper method that creates the application s main window, adds some text to the window, and displays the window

SimpleApplication1.cs
start example
 using System; 
using MSAvalon.Windows;
using MSAvalon.Windows.Controls;
using MSAvalon.Windows.Media;
namespace IntroLonghorn {
public class MyApp : MSAvalon.Windows.Application {
MSAvalon.Windows.Controls.SimpleText txtElement;
MSAvalon.Windows.Window mainWindow;

protected override void OnStartingUp (StartingUpCancelEventArgs e) {
base.OnStartingUp (e);
CreateAndShowMainWindow ();
}

private void CreateAndShowMainWindow () {
// Create the applications main window
mainWindow = new MSAvalon.Windows.Window ();

// Add a dark red, 14 point, "Hello World!" text element
txtElement = new MSAvalon.Windows.Controls.SimpleText ();
txtElement.Text = "Hello World!";
txtElement.Foreground = new
MSAvalon.Windows.Media.SolidColorBrush (Colors.DarkRed);
txtElement.FontSize = new FontSize (14,
FontSizeType.Point);
mainWindow.Children.Add (txtElement);
mainWindow.Show ();
}
}

internal sealed class EntryClass {
[System.STAThread]
private static void Main () {
MyApp app = new MyApp ();
Thread.CurrentThread.ApartmentState = System.Threading.ApartmentState.STA;
app.Run ();
}
}
}

I used the following command line to compile the SimpleApplication1.cs source code into an executable application. You might need to adjust the paths to the referenced assemblies.

 csc /r:C:\WINDOWS\Microsoft.NET\Windows\v6.0.4030\PresentationCore.dll 
/r:C:\WINDOWS\Microsoft.NET\Windows\v6.0.4030\PresentationFramework.dll
/r:C:\WINDOWS\Microsoft.NET\Windows\v6.0.4030\WindowsBase.dll
SimpleApplication1.cs

The Application class contains a number of other useful properties, methods, and events. For example, your application class can override the OnShuttingDown virtual method to provide custom shutdown behavior. The application class also provides the StartingUp and ShuttingDown events so that other classes can register for startup and shutdown notifications. The Shutdown method allows you to initiate the shutdown of the application programmatically.

You might want to reference your application object from multiple places in your source code. Therefore, the Application class provides the Current static property that returns a reference to your application object. The following code fragment uses the Current property to locate the application object and register for a shutdown event notification:

 MyApp app = (MyApp) MSAvalon.Windows.Application.Current; 
app.ShuttingDown += new
Application.ShuttingDownEventHandler (ShutDownHandler);

private static void
ShutDownHandler (object sender, MSAvalon.Windows.ShuttingDownEventArgs e) {

}

The NavigationApplication Class

When you want navigation support for your application, you ll typically use the MSAvalon.Windows.Navigation.NavigationApplication class, which extends the MSAvalon.Windows.Application class. Although you can build a navigation- based application without using the NavigationApplication class, using the class provides the following additional capabilities to your application:

  • Simplifies writing navigation-based applications; not usually necessary to subclass the class

  • Determines when a connection is available

  • Provides navigation events ”such as Navigating , NavigationProcess , Navigated , NavigationError , LoadCompleted , and Stopped ” which it fires when the appropriate event occurs in any of the application s windows

  • Shares state across pages

  • Provides a container for property values shared across pages

  • Implements a policy that opens an initial window by default

Externally, a navigation application s user can navigate only to well- defined entry points of the application. Internally, however, the developer controls navigation by hooking events. You can determine when a window or frame attempts to navigate to a new page and when the navigation is complete. You can cancel or redirect any navigation. You can find out the identity of the target page. You can handle navigation errors.

The familiar navigation model makes an application easy to use. A navigation application provides behavior similar to the Web. Your application can use hyperlinks , provide Forward and Back buttons , display a Favorites list, and maintain a page History. The Longhorn NavigationApplication class and related classes provide all the support for such features.

A navigation application works whether online or offline, and it works the same whether a browser hosts the application or the application runs as a stand-alone. In addition, you have complete control over this Weblike behavior. You can customize the user experience as required. You can insert, remove, and modify Travelog entries to control where the Forward and Back operations go. You can define which pages (entry points) are logged in the History.

A navigation application typically creates one or more instances of the MSAvalon.Windows.Navigation.NavigationWindow class. The SimpleApplication2.cs listing, shown here, demonstrates a use of these classes. This listing is the same as SimpleApplication1.cs except that it uses the NavigationApplication and NavigationWindow classes.

end example
 

[1] Technically, you only need to set the threading state in code-only applications. Applications that use XAML don t need their threading state initialized .




Introducing Microsoft WinFX
Introducing WinFX(TM) The Application Programming Interface for the Next Generation of Microsoft Windows Code Name Longhorn (Pro Developer)
ISBN: 0735620857
EAN: 2147483647
Year: 2004
Pages: 83
Authors: Brent Rector

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net