In my previous post I showed how to set up a generic technique for logging details about the execution of routed commands in a Window. That post’s demo app was written in C#. I’m trying to learn F# and explore how well it works as a language with which to program WPF applications, so I decided to re-write that demo in F#. Since I’m not at all proficient with F# yet (I’ve been studying it for less than a week), I had to drop a few aspects of the original demo simply because I have no clue how to implement them in F# yet!
If you want to run this demo app, you need to install the F# distributable. I am using version 188.8.131.52, which is available here. It can work with Visual Studio 2003, 2005, and 2008.
Here’s the Window1.xaml file:
Here’s the F# code (demo.fs):
If you compare this code with the C# code in my previous post, it’s immediately clear that we’re in a different world here. I cannot pre-compile my Window1 class into a BAML resource because F# projects have no notion of that. Instead I must load the XAML file from disk and pass it to XamlReader.Load to get the Window instantiated (that useful little function came from Robert Pickering’s book “Foundations of F#”). This also means that I cannot have a code-behind file for the Window1 class, unless there’s some trick I’m not aware of yet.
All of the code contained by the Window in the C# version now lives outside of it, because Visual Studio won’t let me have an F# code-behind file at this point in time. Eventually that might be addressed, but for now it kind of throws a monkey wrench into the whole “encapsulation” thing.
I really like how the calls to CommandManager functions in the attachedCommandLogger function are set up. The syntax for passing an anonymous function in F# is very nice, and I look forward to using the similarly elegant lamba syntax in C# 3.0 too. Since F# influenced many of the modern additions to C#, learning F# feels like killing two birds with one stone.
Programming in F# just feels “light” somehow. I like it.
I don’t think that this demo is in-depth enough yet to persuade me either for or against using F# with WPF. Once I have a better grasp on the language, and the concepts, I will be able to make a better decision about that. Until then I’m just enjoying WPF and F#!!
You can download the demo app here: Command Logging in F# (demo app) Be sure to change the file extension from .DOC to .ZIP and then decompress the file.