Hi falcon,
You can start with the following code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
 

#light

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

#I @"C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.0"
#r @"PresentationCore.dll"
#r @"PresentationFramework.dll"
#r @"WindowsBase.dll"

// Create window and application
let app = new Application()
let win = new Window(Width = 300.0, Height = 300.0, Title = "F# WPF Sample", Visibility = Visibility.Visible)
let canv = new Canvas()
win.Content <- canv; 

// Function creates new line object
let float = Float.of_int32
let addLine (x1, y1) (x2, y2) = 
  let l = new Shapes.Line(X1 = float x1, Y1 = float y1, X2 = float x2, Y2 = float y2)
  l.Stroke <- Media.Brushes.DarkGreen;
  canv.Children.Add(l) |> ignore

// Main - start the application  
[<STAThread>]
do
  let r = new Random();
  for i = 1 to 10 do
    addLine (r.Next(300),r.Next(300)) (r.Next(300),r.Next(300)) 
  done;  
  win.Closed.Add( fun _ -> app.Shutdown(0) )
  app.Run(win) |> ignore

It will work only when you execute it as an application (Ctrl+F5), so you can't play with this interactively from FSI (if you wanted this you'd have to type #use @"load-wpf.fsx" from the F# samples directory).

Drawing a line in WPF is a bit difficult :-) because WPF is based on composition of basic elements, so we need to create line as an object and put it on the Canvas.

Also this sample creates everything manually, but the power of WPF is XAML (declarative XML based markup used for creating the interface). You can find some good samples that use XAML on Robert Pickering's website.

By on 4/24/2007 4:24 PM ()

Thanks! I'll try it as soon as I get to my windows machine. I'm hoping that I can create simple functions (combinators?) for line, point, curve, polygon, etc. and then combine them to create shapes, perhaps interactive, animated shapes, without having to deal with all the mutable references

By on 4/25/2007 8:32 PM ()

Thanks! I'll try it as soon as I get to my windows machine. I'm hoping that I can create simple functions (combinators?) for line, point, curve, polygon, etc. and then combine them to create shapes, perhaps interactive, animated shapes, without having to deal with all the mutable references

Yes. You want an immutable scene graph that is rendered for you. We've been working on a commercial library that implements this. Check out the demos for the free beta release on our site:

[link:www.ffconsultancy.com]

Cheers,
Jon.

By on 5/4/2007 8:42 PM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper