Walking the visual tree in F#

This blog post shows how to dump the visual tree into a console window, using F#.

In my continued quest to understand how F# and WPF can work well together, I wrote a simple utility which walks down the visual tree of a UI and writes it out to a console window.  I found that F# is a natural fit for this type of quick script-like coding.  This app is not imminently useful, but it is a great way to both learn F# and get used to manipulating WPF with F#.

Here is the F# code:

Walk visual tree in F# (code)

Notice in the ‘createWindow’ function it uses the ‘use’ keyword.  That is a relatively new addition to the language, which you can read more about here.  It’s like a ‘using’ block in C#.

When the app runs, it loads a Window from a XAML file.  That XAML looks like this:

Walk visual tree in F# (xaml)

When you run the app, you will see both a console window and the window which was loaded from the XAML file.  Here is a screenshot:

Walk visual tree in F# (screenshot)

Download the demo app here: Walk visual tree in F# (demo app)  Be sure to change the file extension from .DOC to .ZIP and then decompress the file.  Download the F# compiler and tools here.

About these ads

6 Responses to Walking the visual tree in F#

  1. Josh,

    You are really getting into F#!

    I remember the LET statement from a language I learned a long time ago. Ummm, let me remember. Oh yea, BASIC man!

    May have to look into this F# business,

    Cheers,

    Karl

  2. Josh Smith says:

    Karl,

    F# borrows things from many different languages, and programming styles. It’s a fascinating language to learn, because it allows you to think about things in many different ways. You’re not “trapped” in the OO mindset.

    Josh

  3. Christer says:

    How would this look in C#?
    In what way is this small app better in F#?

  4. Josh Smith says:

    Christer,

    I’m not claiming that this small app is “better” in F# than in C#. I am just showing how to work in WPF using F#, and getting some practice with the language.

    Josh

  5. free download of Visual Styles windows vista Themes

    no kidding!

Follow

Get every new post delivered to your Inbox.

Join 285 other followers

%d bloggers like this: