WPF & F#

In this post I would like to write a few words about creating user interfaces to F# programs.

I don’t know about constructors for GUI, and I’m not sure about their existance at all, what’s why in this article we will create UI using objects of .NET Framework 3.0;

At first, we need to add some references to our project to make WPF available. This references are: PresentationCore, PresentationFramework and WindowsBase (all of dll’s are located at C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.0\)

type ResultWindow = class
    inherit Window

Here we are creating a class called ResultWindow, that inherites defined in the framework class Window.

val mutable private minX : float

I use val keyword for declaration of the field. It’s much more simple to use mutable structures instead of immutable, but I need to notice that you shouldn’t use mutable stuctures in pure functional code. All of class members declared with val keyword must be defined in constructor.

new ( (* parameters here *) ) as this = {
        (* definition of fields *)
        minX = 0.0
                                     } then
        (* some other actions *)

Now it’s time to create a method (static methods are created without this. prefix).

member this.DrawGrid =
        let gridLine = new Line()
        (* and many-many lines of code *)

If you are not familiar to WPF my advise is to read some books about it. For example:

  • Don Syme’s WebLog
  • Practical WPF Graphics Programming — Jack Xu, Ph.D
  • WPF Recipes in C# 2008 — Sam Noble, Sam Bourton, Allen Jones
  • At the pictures presented below you can see analytic and neural solution graphics:


    I don’t publish my code in this post, because it is quite large. You can download it.

    Leave a comment

    Your comment