109 people like it.

Load XAML

This example shows how to load a Xaml file allowing to use WPF from F#. It also shows how to access WPF objects and register event handlers.

 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: 
open System.Windows
open System.Windows.Controls
open System.Windows.Markup
open System.Windows.Media
open System.Xml

let loadXamlWindow (filename:string) =
  let reader = XmlReader.Create(filename)
  XamlReader.Load(reader) :?> Window

let app = new Application()

// Load the window.xaml file
let w = loadXamlWindow("window.xaml")
w.Show()

// We assume that there is an ellipse named Circle
let e = w.FindName("Circle") :?> Ellipse

// Register an event handler
e.MouseLeftButtonUp.Add(fun _ ->
  e.Fill <- 
    if e.Fill = (Brushes.Yellow :> Brush) then Brushes.Red
    else Brushes.Yellow
)
namespace System
namespace System.Windows
namespace System.Xml
val loadXamlWindow : filename:string -> 'a

Full name: Script.loadXamlWindow
val filename : string
Multiple items
val string : value:'T -> string

Full name: Microsoft.FSharp.Core.Operators.string

--------------------
type string = System.String

Full name: Microsoft.FSharp.Core.string
val reader : XmlReader
type XmlReader =
  member AttributeCount : int
  member BaseURI : string
  member CanReadBinaryContent : bool
  member CanReadValueChunk : bool
  member CanResolveEntity : bool
  member Close : unit -> unit
  member Depth : int
  member EOF : bool
  member GetAttribute : name:string -> string + 2 overloads
  member HasAttributes : bool
  ...

Full name: System.Xml.XmlReader
XmlReader.Create(input: System.IO.TextReader) : XmlReader
   (+0 other overloads)
XmlReader.Create(input: System.IO.Stream) : XmlReader
   (+0 other overloads)
XmlReader.Create(inputUri: string) : XmlReader
   (+0 other overloads)
XmlReader.Create(reader: XmlReader, settings: XmlReaderSettings) : XmlReader
   (+0 other overloads)
XmlReader.Create(input: System.IO.TextReader, settings: XmlReaderSettings) : XmlReader
   (+0 other overloads)
XmlReader.Create(input: System.IO.Stream, settings: XmlReaderSettings) : XmlReader
   (+0 other overloads)
XmlReader.Create(inputUri: string, settings: XmlReaderSettings) : XmlReader
   (+0 other overloads)
XmlReader.Create(input: System.IO.TextReader, settings: XmlReaderSettings, inputContext: XmlParserContext) : XmlReader
   (+0 other overloads)
XmlReader.Create(input: System.IO.TextReader, settings: XmlReaderSettings, baseUri: string) : XmlReader
   (+0 other overloads)
XmlReader.Create(input: System.IO.Stream, settings: XmlReaderSettings, inputContext: XmlParserContext) : XmlReader
   (+0 other overloads)
val app : obj

Full name: Script.app
val w : obj

Full name: Script.w
val e : obj

Full name: Script.e
Next Version Raw view Test code New version

More information

Link:http://fssnip.net/H
Posted:13 years ago
Author:Antonio Cisternino
Tags: wpf , xaml