Using WPF with F#

by Gregor Uhlenheuer on November 22, 2011

I just recently tried to build a small WPF application in F# and although Visual Studio 2010 does not provide great support for WPF in concerns of F# the experience was not as bad as expected.

I will give you a short walkthrough on how to create a new F# project in Visual Studio and how to use WPF in there.

Create a new project

The first thing you have to do is to create a new F# application in Visual Studio.

  1. Use File → New → Project… to create a new project
  2. Select Visual F# → F# Application
  3. Add the following additional references:
    • PresentationCore
    • PresentationFramework
    • System.Xaml
    • System.Xml
    • WindowsBase
  4. Open the Properties of your newly created project

  5. Select Windows Application as Output type (in the Application tab)

Application’s entry point

Basically you are now ready to start hacking a fine WPF application in F#. In order to define your application’s entry point you may want to add the following lines in your main source file:

Helper functions

In order to conveniently handle the WPF controls I found a few helper functions to be very handy:

Create a window

The last thing you have to do is to create a new XAML file, add its BuildAction to Resource and add the matching code-behind F# source file. You find a small example down below:

The code-behind F# source file:

That’s pretty much all you need to get started with your WPF hacking in F#. I was very surprised how the creation of a small application like this was not much more complicated than it would have been in C#.

So you don’t have to be too frightened by Visual Studio 2010 not shipping a builtin project template or sophisticated support for WPF and try it out yourself!


This post is tagged with f#, wpf, .net and programming