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:
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:
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:
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.
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
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
Cool stuff!
How would this look in C#?
In what way is this small app better in F#?
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
free download of Visual Styles windows vista Themes
no kidding!