Description
This article gives a background to build procedural application for Windows Vista (codename known as Longhorn). The simple application that would be built as we go will give a developer a good understanding of how to go about writing a procedural application.
Introduction
The sample code with this article is written using the Longhorn Build 4074 (Released at WINHEC).
There are many articles telling how to write applications using XAML (ZAMAL) for longhorn and a very few giving details on how to write an winform application in the way we always have been writing. The procedural way of writing a longhorn application is not new to a .NET developer. BY procedural way i mean, writing your application totally in c#, which is the way we have been writing applications in managed code. Hence, i write this article to demystify this way of writing applications.
XAML applications are not always the way to build applications on longhorn. In many cases a developer does not want to use a declarative method (XAML application) of writing applications {Usually when you want to decide the look and feel of your application in runtime.}. For such cases we can write applciations totally in c#. One might wonder that, every developer would now need to learn two different methodologies to build applications. But the good part is that a procedural application uses the same model as an XAML application. In a procedural application a developer initializes the same classes for which he may use tags in the XAML way. For example, if you use a <Dockpanel></DockPanel> tag in your XAML application, you would inturn use System.Windows.Controls.DockPanel class while writing a procedural application.
Let's look at the sample code of a procedural application.
Now lets tear apart the sample code and look at it piece by piece.
You would find that among the various namespaces that are imported in this sample, the System.Windows.Navigation is a new one. This name space contains the various classes that are used for navigation in applications, which includes navigating between windows or pages.
Notice that the class MyFirstApplication is inherited from the Application class. Application class is a light-weight class compared to the NavigationApplication class. The Application class is usually the class that is used the least, as this kind of applications lack navigation. In other words, these applications cannot create windows and pages in their applications. For Applications where you will need navigation support (which in most cases you will) you will have to inherit the NavigationApplication class.
Also, you would notice a sealed class in this code called MyMain with a main method called Main. This calss is used to launch the application. The application object has several events. I have overridden a event from application class to implement a custom OnStartingUp procedure. The other events that are available with the application object are Activate, Deactivate, SessionEnding, and ShuttingDown.
using System;
using System.Windows;
using System.Threading;
using System.Windows.Navigation;
namespace MyFirstProceduralApplication
{
public class MyFirstApplication : Application
{
System.Windows.Controls.DockPanel dockPanel;
System.Windows.Controls.DockPanel topPanel;
System.Windows.Controls.TextBox textBox;
System.Windows.Controls.Button button;
System.Windows.Controls.Button closeButton;
System.Windows.Controls.ListBox listBox;
System.Windows.Controls.Image image;
System.Windows.Window window;
// Override the OnStartingUp method to have custom start up routine.
protected override void OnStartingUp(StartingUpCancelEventArgs e)
{
// Get an instance of the Window object where the UI will be hosted.
window = new NavigationWindow();
// dockPanel will be used as the dock panel where all the application UI
// will be contained. Also it shall be the root to the UI Tree.
dockPanel = new System.Windows.Controls.DockPanel();
dockPanel.Height = new Length(1500);
// Initilizing all the controls needed in the application.
topPanel = new System.Windows.Controls.DockPanel();
textBox = new System.Windows.Controls.TextBox();
button = new System.Windows.Controls.Button();
closeButton = new System.Windows.Controls.Button();
listBox = new System.Windows.Controls.ListBox();
image = new System.Windows.Controls.Image();
// Set Values for the different controls.
textBox.Text = "Here goes some text";
textBox.Height = new Length(30);
textBox.Width = new Length(300);
button.Content = "Click Me";
button.Click += (this.Button_Clicked);
// Add the controls in to the top panel.
topPanel.Children.Add(textBox);
topPanel.Children.Add(button);
System.Windows.Controls.DockPanel.SetDock(textBox, System.Windows.Controls.Dock.Left);
System.Windows.Controls.DockPanel.SetDock(button, System.Windows.Controls.Dock.Right);
// Add the topPanel to the root DockPanel
dockPanel.Children.Add(topPanel);
System.Windows.Controls.DockPanel.SetDock(topPanel, System.Windows.Controls.Dock.Top);
listBox.Items.Add("Item1");
listBox.Items.Add("Item2");
listBox.Items.Add("Item3");
listBox.Items.Add("Item4");
listBox.Height = new Length(1000);
dockPanel.Children.Add(listBox);
System.Windows.Controls.DockPanel.SetDock(listBox, System.Windows.Controls.Dock.Left);
closeButton.Content = "Exit";
closeButton.Height = new Length(1000);
closeButton.Click += (this.End_App);
dockPanel.Children.Add(closeButton);
System.Windows.Controls.DockPanel.SetDock(closeButton, System.Windows.Controls.Dock.Right);
// Insert the image.
image.Height = new Length(1000);
image.Width = new Length(800);
image.Source = new System.Windows.Media.ImageData(new System.IO.FileStream(@"image.jpg",System.IO.FileMode.Open));
dockPanel.Children.Add(image);
System.Windows.Controls.DockPanel.SetDock(image, System.Windows.Controls.Dock.Fill);
window.Content = dockPanel;
window.Show();
}
private void Button_Clicked(object obj, System.Windows.Controls.ClickEventArgs e)
{
MessageBox.Show("Hey i was Just Clicked !!!");
}
private void End_App(object obj, System.Windows.Controls.ClickEventArgs e)
{
MessageBox.Show("Hey i am going DOWN !!!");
System.Windows.Application myApp = System.Windows.Application.Current;
myApp.Shutdown();
}
}
internal sealed class MyMain
{
[System.STAThread()]
public static void Main()
{
Thread.CurrentThread.ApartmentState = System.Threading.ApartmentState.STA;
MyFirstApplication myFirstApplication = new MyFirstApplication();
myFirstApplication.Run();
}
}
}
Below is the screen shot of how the application will look after it is compiled.

<Project DefaultTargets="Build">
<PropertyGroup>
<Property Language="C#" />
<Property DefaultClrNameSpace="MyFirstProceduralApplication" />
<Property TargetName="MyFirstApplication" />
<Property HostInBrowser="False" />
<Property ProductVersion="8.0.30703" />
<Property SchemaVersion="2.0" />
<Property ProjectGuid="{F93695FC-EAE2-471B-AD1E-3F9515FD42BD}" />
</PropertyGroup>
<Import Project="$(LAPI)\WindowsApplication.target" />
<ItemGroup>
<Item Type="Compile" Include="MyFirstApplication.cs" SubType="Code" />
</ItemGroup>
<ProjectExtensions>
<VisualStudio>
<CommonProperties>
<FL_FAE04EC0_301F_11D3_BF4B_00C04F79EFBC />
</CommonProperties>
<UserProperties />
</VisualStudio>
</ProjectExtensions>
</Project>
Below is the .proj file to build the above sample.
To build the application from the command line you will have to do the following.
(I am assuming that you have longhorn sdk's Bin folder part of the PATH environment variable)
c:\>SetEnv.cmd
c:\>msbuild MyFirstApplication.proj
(Tip: Use msbuild MyFirstApplication.proj /v:diag to build in verbose mode with all the diagnostic messages, to know what happens behind the scenes during the build process.)
Summary
This article gives a background to build procedural application for longhorn. The simple application that would be built as we go will give a developer a good understanding of how to go about writing a procedural application.