Logging Routed Commands in F#

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, which is available here. It can work with Visual Studio 2003, 2005, and 2008.

Here’s the Window1.xaml file:

Command Logging in F# (xaml)

Here’s the F# code (demo.fs):

Command Logging in F# (code)

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.

3 Responses to Logging Routed Commands in F#

  1. Steve says:

    Just a little question, what does the |> ignore do? Ignore all exceptions?

  2. Josh Smith says:


    That symbol is called the “pipe forward”, “pass forward”, or “forward” operator. It is used here because the StringBuilder methods return a value, not void. The F# compiler barks if a function returns a value but you don’t use it, so you need to forward it along to a special “black hole” function called ignore.


  3. […] Smith wrote an interesting post today on Logging Routed Commands in F#Here’s a quick […]

%d bloggers like this: