This article introduces you to programming for the next version of Windows codenamed Longhorn. (In order to understand the codename jargon, please read The perplexed guide for the new codenames of Microsoft.) Now getting back to the point, Longhorn when released will change the landscape for developers in a significant way. Although most of the old stuff from COM (isn’t that old enough), .NET (Bye Bye WinForms) will work smoothly, the new technologies such as Avalon, WinFS, Indigo provide a much better and richer base to work with.
I will take you through a very simple program, which responds to a click on a button control and displays "Hello Longhorn". The code is entirely written in C#. I have used MSBuild to compile my code (I still do not have my hands on Whidbey). In a sequel to this article, I will show you how to write the same program in a new revolutionary way using XAML. But for now, let’s stick to vanilla C#.
Getting to the business
The figure below is what our killer application will look like. By looking at it, if you figured out that it has a window, a button and a text box, then pat yourself, because that’s what there is to it. It has another goodie called
Canvas, and I will tell you about it in a minute. Here’s the picture:
Okay, now that you are done looking at this piece of exceptional artistry, I will show you how it’s done.
As usual, we start with our import statements:
Next, we create a class:
public class TextBoxApp : Application
Application is the top-level object, and all applications should inherit from this class. You can also inherit from the
NavigationApplication class for Longhorn Navigation Applications.
Next, I declare a few controls:
Although I have my import statements at the top, I still like to use the full namespace. I feel that this helps me in learning about the new framework.
We then override a method from the base class which is the
Application class. This method is always called when an application is started.
protected override void OnStartingUp(StartingUpCancelEventArgs e)
Now, we will implement our private method, which is the meat of the application. This method will create a window, put a
Canvas and then put other controls on top of the
Canvas class, which is located in the
MSAvalon.Windows.Controls namespace, is one of the panels available in Avalon (the new presentation system under Longhorn). Below is the complete method:
private void CreateWindow()
MSAvalon.Windows.Window myWindow = new MSAvalon.Windows.Window();
myWindow.Width = new Length(300);
myWindow.Height = new Length(170);
myCanvas = new MSAvalon.Windows.Controls.Canvas();
myCanvas.Background = MSAvalon.Windows.Media.Brushes.LightSteelBlue;
myCanvas.Width = new Length(100,UnitType.Percent);
myCanvas.Height = new Length(100,UnitType.Percent);
myTextBox = new MSAvalon.Windows.Controls.TextBox();
myTextBox.Width = new Length(200);
myTextBox.Height = new Length(40);
MSAvalon.Windows.Controls.Canvas.SetTop(myTextBox, new Length(30));
MSAvalon.Windows.Controls.Canvas.SetLeft(myTextBox, new Length(50));
myButton = new MSAvalon.Windows.Controls.Button();
MSAvalon.Windows.Controls.Canvas.SetTop(myButton, new Length(100));
MSAvalon.Windows.Controls.Canvas.SetLeft(myButton, new Length(110));
myButton.Content = "Click Me";
myButton.Click += new ClickEventHandler(OnClick);
I will point your attention to a particular line in the code:
myButton.Content = "Click Me";
Note that there is no
Text property for the
I have also specified an event handler for
Click event. All that this handler will do is change the text in my text box:
void OnClick(object sender, ClickEventArgs e)
myTextBox.Text = "Hello Longhorn";
Next, we need to provide a
main method that will run our application. I have my
main method in my
EntryClass. I have marked it as
sealed, because I never wish to inherit from this class (you don’t need to do this).
public sealed class EntryClass
private static void Main ()
TextBoxApp app = new TextBoxApp ();
As I do not have Whidbey, I used MSBuild to compile my project. You will need a project file, which is included in the code download with this article. A project file tells MSBuild about what kind of application this is, what files are included, and a few other things. Make sure that you have both project and the source files in the same folder.
In order to build the application successfully, launch your build environment from Start - All Programs - Microsoft Longhorn SDK - Open Build Environment Window - Longhorn Build Environment. Change to the directory where you have the project and the source file, and type:
and hit Enter. You should have an EXE in the Release folder under your current directory. Click on the EXE and enjoy the application.
For all Longhorn nuts, I recommend a daily visit to Longhorn Developer Centre.