Building a WPF Application (XAML-free)

Now that you better understand the core services provided by Application and Window, it is time to build a simple
WPF application. This application will not be created using XAML (recall that XAML is technically optional).
Rather, this application will take the ‘100% code’ approach.

Although WPF application are not required to use XAML, a pure code approach is not necessarily recommended.
This approach does not honor the separation of concerns between GUI layout and functionality. However, it is
important to understand how to work with WPF types in code, even when you are making use of XAML.

Recall that the System.Windows.Application type represents a running instance of a WPF app. Unlike Windows
Forms, the
Application type exposes its core functionality as instance-level (rather than static) members. Thus, a
WPF app will have a class type that extends
Application.

The other critical type is Window, which represents a topmost window or dialog box. This type is typically
subclassed for strong typing. However, this is not required.

Consider the following simple C# WPF app that handles some key events. The
Startup event makes use of the
StartupEventHandler delegate. The Exit event makes use of ExitEventHandler.

It also deals with any unhandled exceptions by handling the
DispatcherUnhandledException event. This event
makes use of the
DispatcherUnhandledExceptionEventHandler delegate, which is a member of the System.
Windows.Threading namespace. Notice the incoming
DispatcherUnhandledExceptionEventArgs gives access to
the error information and provides a way to clear the exception.


// C#
// A Simple WPF Application written without XAML.
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Threading;

namespace SimpleWPFApp
{
class MyWPFApp : Application
{
[STAThread]
static void Main()
{
  // Handle key events and then run the application.
  MyWPFApp app = new MyWPFApp();
  app.Startup += new StartupEventHandler(AppStartUp);   
  app.Exit += new ExitEventHandler(AppExit);
  app.DispatcherUnhandledException += new
    DispatcherUnhandledExceptionEventHandler(AppUnhandledException);

  // Fires the Startup event.
  app.Run();  
}

static void AppUnhandledException(object sender,  
  DispatcherUnhandledExceptionEventArgs e)
{
  MessageBox.Show(e.Exception.Message, "Unhandled error!!");
  e.Handled = true;
}

static void AppExit(object sender, ExitEventArgs e)
{ MessageBox.Show("App has exited"); }

static void AppStartUp(object sender, StartupEventArgs e)
{
  // Create a Window object and set some basic properties.
  Window mainWindow = new Window();
  mainWindow.Title = "My First WPF App!";
  mainWindow.Height = 200;
  mainWindow.Width = 300;
  mainWindow.WindowStartupLocation =
     WindowStartupLocation.CenterScreen;
  mainWindow.Show();
}
}
}



In VB, an Application-derived type would look like the following example. Notice it makes use of the AddHandler
statement to hook into events. This is necessary because local variables (e.g., the Application object) cannot be
declared using the
WithEvents keyword.


' VB
' A Simple WPF Application, written without XAML.
Imports System
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Threading

Namespace SimpleWPFApp
' This first example defines a single class type to
' represent the application itself and the main window.
Class MyWPFApp
Inherits Application

<STAThread> _
Shared Sub Main()    
  ' Handle key events and then run the application.
  Dim app As New MyWPFApp()
  AddHandler app.Startup, AddressOf AppStartUp
  AddHandler app.Exit, AddressOf AppExit
  AddHandler app.DispatcherUnhandledException, AddressOf AppUnhandledException
  app.Run()   
End Sub

Shared Sub AppUnhandledException(ByVal sender As Object, _
     ByVal e As DispatcherUnhandledExceptionEventArgs)
  MessageBox.Show(e.Exception.Message, "Unhandled error!!")
  e.Handled = True
End Sub

Shared Sub AppExit(ByVal sender As object, ByVal e As ExitEventArgs)
  MessageBox.Show("App has exited")
End Sub

Shared Sub AppStartUp(ByVal sender As Object, _
ByVal e As StartUpEventArgs)
  ' Create a Window object and set some basic properties.
  Dim mainWindow As new Window()
  mainWindow.Title = "My First WPF App!"
  mainWindow.Height = 200
  mainWindow.Width = 300
  mainWindow.WindowStartupLocation = WindowStartupLocation.CenterScreen
  mainWindow.Show()
End Sub
End Class
End Namespace



Here is a strongly typed Window-derived class that encapsulates the details of the main window. Notice it extends
Window and sets core properties via constructor arguments. VB programmers would author the same code
statements within a
Sub New().


// C# (VB code would be similar.)
class MainWindow : Window
{
public MainWindow(string windowTitle, int height, int width)
{
 this.Title = windowTitle;
 this.WindowStartupLocation = WindowStartupLocation.CenterScreen;
 this.Height = height;
 this.Width = width;
 this.Show();
}
}


At this point, you can update the Startup event handler. Note you are now creating a strongly typed MainWindow
object.


// C# (VB code would be similar.)
static void AppStartUp(object sender, StartupEventArgs e)
{
// Create a MainWindow object.
// VB code would use the Dim keyword:
// Dim wnd as New MainWindow("My better WPF App!", 200, 300)
MainWindow wnd = new MainWindow("My better WPF App!", 200, 300);
}
Building a WPF Application
Table of Contents
Copyright (c) 2008.  Intertech, Inc. All Rights Reserved.  This information is to be used exclusively as an
online learning aid.  Any attempts to copy, reproduce, or use for training is strictly prohibited.