Posted by: Zeeshan Amjad | July 27, 2009

WPF Programming with F#


 

F# is a mixture of functional and imperative programming language introduced by Microsoft Research and now released as a first class citizen of .Net family. F# is not fully functional programming language; one can even crate classes and do object oriented programming in it too. Due to this mix nature of F# it is very easy to use it with other .Net technologies and class libraries.

Here is our first and the simplest possible WPF program written in F#

#light

open System
open System.Windows

[<STAThread>]
do
 MessageBox.Show("Hello World From F#", "F# is Cool") |>ignore

This program just display one message box. For any practicle WPF program, we need to crate instance of at least two classes named Window and Application. Here is the simple program to crate instance of Window and Applicatin classes and create one Window with caption “Hello World From F#”.

#light

open System
open System.Windows

[<STAThread>]
do
 let window = new Window()
 window.Title <- "Hello World From F#"
 window.Height <- 300.0
 window.Width <- 400.0
 
 let app = new Application() in 
 app.Run(window) |> ignore

We can inherit classes from Window and Application to store application and main window specific information. Here is a simple program to show the inheritance of WPF classes in F#

#light

open System
open System.Windows

type MyWindow() = inherit Window()

type MyApplication() = inherit Application()

[<STAThread>]
do
 let window = new MyWindow()
 window.Title <- "Hello World from F#"
 window.Height <- 300.0
 window.Width <- 400.0
 
 let app = new MyApplication() in 
 app.Run(window) |> ignore

We can even set the properties inside the constructor rather than outside the class. Here is a simple program to demonstrate this concept.

#light

open System
open System.Windows

type MyWindow() = inherit Window(Title = "Hello World from F#", Height = 300.0, Width = 400.0) 

type MyApplication() = inherit Application()

[<STAThread>]
do
 let window = new MyWindow()
 
 let app = new MyApplication() in 
 app.Run(window) |> ignore

In similar way we can add windows control in F# program. Here is the simple program to show the usage of button control inside the window.

#light

open System
open System.Windows
open System.Windows.Controls

type MyWindow() = inherit Window(Title = "Hello World from F#", Height = 300.0, Width = 400.0) 

type MyApplication() = inherit Application()

[<STAThread>]
do
 let window = new MyWindow()
 
 let button = new Button()
 button.Content <- "Press Me"
 button.Margin <- new Thickness(10.0)
 
 window.Content <- button
 
 let app = new MyApplication() in 
 app.Run(window) |> ignore

If we want to add message handler then we can do it very easily with anonymous function. Here is a simple code for this

#light

open System
open System.Windows
open System.Windows.Controls

type MyWindow() = inherit Window(Title = "Hello World from F#", Height = 300.0, Width = 400.0) 

type MyApplication() = inherit Application()

[<STAThread>]
do
 let window = new MyWindow()
 
 let button = new Button()
 button.Content <- "Press Me"
 button.Margin <- new Thickness(10.0)
 button.Click.Add(fun _ -> MessageBox.Show("You got it", "From Button") |> ignore)
 
 window.Content <- button
 
 let app = new MyApplication() in 
 app.Run(window) |> ignore

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: