Wednesday, January 24, 2007

Windows Presentation Foundation


It's been a while since my last post. I guess I figured out I won't make a living out of this... he he, just kidding. Anyways, you know the holiday season and all, it's hard to sit down and write, so now, as I wait for the "Doble San Antonio" to come. I thought I'd write something.

Since .net Framework 3.0 (formerly winfx) was released I've been reading about it and now I'll post a little Hello World WPF application. There are tons of papers about WPF and XAML (Zamel) so I won't dig into it.

After I installed the .net 3.0 extensions for VS2005 I wanted to create my first Hello World application consisting in a simple form with a button that once clicked a MessageBox would appear with the catchy phrase, "Hello World". Simple right? Not as simple as it sounds, or at least, not as simple as it used to be.

When you start a WPF application you get the main form in the designer. There are a few improvements there like a rule for aligning controls and stuff. Then I went to my toolbox (many new controls) and drag a button into the form.

While doing this you can see what the designer is writing in XAML in a little window below the designer view. You can work either with the designer and the property page or writing the XAML code yourself.

So, once I had my button on the form I did the obvious thing to do, double clicked on it to write the handler for the OnClick event, right? WRONG!!! you can double click, triple click, and do as many clicks as you want and nothing will happen. After a few tries I discovered the Click attribute on the button tag (XAML) so I went and wrote the name of the function I wanted the button to fire up. After doing that I went to the code behind file and implemented that function (you can also write it in the XAML document). Keep in mind that this method uses the ClickEventHandler, sou you'll have to write it with a matching signature (object sender, CleickEventArgs e).
Finally, write the code to execute the MessageBox (like in 2.0) and voila!, your first WPF XAML app is working.

I added some other controls and stuff in my example, so feel free to download it and take a look at it. Any comments about it are welcome.

It's been a few days since I started to write the post, I did well in the "Doble San Antonio", thank you, and I also added some interesting functionality to the example like binding.

No comments: