WinFx TestDrive with Boo
Learning the ins and outs of WPF is like climbing Mount Apo for the first time - minus a guide. Oh yes, judging by the sample applications you see on the Internet, Microsoft may have succeeded in creating a very compelling framework that will change the face of computing as we know it today. The engineers who dared build this thing has probably thrown in every piece of computing gobbledygook ever imagined - including the proverbial kitchen sink.
That kitchen sink may very well be the new markup language called XAML, which those bloody engineers lovingly pronounce as zammel. The fact that it is being hyped in every WPF literature as the de facto standard in writing the next generation UIs leaves me utterly breathless. WPF is all about this new markup (XAML) and code-behind (C# or VB.NET) - similar to the combo made popular in WebForms.
Whatever similarities between WebForms and WPF abruptly ends there. Even a six year .NET developer won't have any comfort zones if that's any consolation. I once had this illusion that since WPF is basically .NET, I will be hitting the ground running by the time I decided to study WPF. It actually felt more like being hit by a runaway truck. Microsoft did err when they labeled WinFX as .NET3. They should have concatenated a higher number - like .NET5 perhaps.
There are a thousand ways to skin a cat . XAML is first translated to raw C# or VB.NET code during the compilation process. This gives those who are zammel challenged like me, a glimmer of hope that perhaps learning the new mark up can be postponed until a much much later date. That old aphorism may still apply to programming WPF applications. Hope indeed has an uncanny way of manifesting itself even during the bleakest of moments!
If you've been watching the events that have been taking place in the .NET sphere of late, you'll probably notice that there seems to be a language renaissance of sorts. Phython, Ruby, PHP, Smalltalk and other obscure languages, new and old, are in the process or have been successfully ported to .NET. What was once considered a second rate Java VM copycat is fast becoming the United Nations of programming languages.
Among these new .NET languages, Boo is my favorite. It has a very compact syntax, yet it does not compromise anything. It belongs to a new breed of smart programming languages designed specifically for .NET that has been slowly emerging from the underground. The value of Boo becomes evident when you are prototyping something. And that is exactly what most of us will be doing with WPF between now and next year, writing prototype applications to test the waters so to speak.
To start writing WPF applications sans XAML with Boo (or even c#), start by creating a new Boo WinForm application and then delete the dummy WinForm file. Delete all references except Boo.Lang.Useful (you dont need this in c#) and System, then add references to PresentationCore, PresentationFramework and WindowsBase. Add a new Empty Boo File then type the following code like so:
namespace Boo.WinFx
import System
import System.Windows
import System.Windows.Navigation
[STAThread]
def Main():
# create a window
win = NavigationWindow()
# create an application
app = Application()
# the first window to be shown becomes the main window
win.Show()
# fire the application
app.Run()
Hit F5 to run this barebones WinFx Navigation Application. Notice that we did not enclose the Main function inside a class, yet the program did execute as expected. This is one of the facets of Boo that makes it special. It adopts very well to procedural style of programming.
There are two types of windows we can use in our applications - the Window and the NavigationWindow. The Window class looks like the old WinForm but that's where their similarities end. The NavigationWindow class inherits from the Window class and extends it with content navigation support. With it we now can create desktop applications that mimic the fuctionality of web applications, without really trying.
Let's say I would like to build an application that displays a button on the first page. If I click on the button, the app will then navigate to the second page. To do this modify the original code like so:
namespace Boo.WinFx
import System
import System.Windows
import System.Windows.Controls
import System.Windows.Navigation
[STAThread]
def Main():
# create a window host
win = NavigationWindow()
# create a textblock
para = TextBlock(Text:"Hello World!!!", FontSize:36)
para.VerticalAlignment = VerticalAlignment.Center
para.HorizontalAlignment =HorizontalAlignment.Center
# create a button
button = Button(Content:"Click Me", Height:30, Width:100)
# display the textblock when the button is clicked
button.Click += { win.Navigate(para) }
# display the button first
win.Navigate(button)
# create an application host
app = Application()
# show the window
win.Show()
# fire the application
app.Run()