The very first application I attempted to write in WPF was a utility to help me with choosing fonts. I'm a developer who has one foot in the designer world, and I am always looking for just the right font. I think that was about 2 years ago, and I was ecstatic that my prototype application took about 15 minutes to write and contained 3 or 4 lines of code (that's not counting the XAML markup.)
When Rob and I began outlining our upcoming book on WPF, we decided to structure the it around four example applications. The font viewer seemed like a natural choice for the first application because it's so simple to write., and easy to understand. I showed the source to Rob, and in less than 10 minutes we had a 100% XAML version of the application. Yes, we removed those troublesome 3-4 lines of code. :-)
I decided to play around with it some more, and here is the result. It's a 24KB text file containing just XAML. If you have .NET 3.5 installed, it should execute in your browser (both Firefox and Internet Explorer work). It's a very simple application, but I think it's fun. I'm especially fond of the built-in tutorial (be sure to click the help icon).
This version is a lot more complicated than the one we build in the book. We don't introduce the concepts of styles, templates, animation, etc until after we're done with the font viewer.
In a nutshell, here is how it works. The list box is bound to the set of installed fonts on your system (System.Windows.Media.Fonts.SystemFontFamilies). There are series of four text blocks, whose font family is bound to the selected item on the list box. Finally, the four text blocks get their content from the text box at the bottom. That's it.
Please feel free to analyze, dissect, assimilate as you like. You can get to the source using your browser's "view source" feature.
Disclaimer: Only very recently did I bother to see if anyone else had done the same thing. Yes, they had, and more.
Bonus Question: How would you handle building an application like this with TDD\BDD? How does declarative programming collide with TDD\BDD?
Posted
02-03-2008 1:19 AM
by
Christopher Bennage