If I understand it correctly, everything you can do using XAML can be done entirely using code.

Here's a simple XAML-file:

1
2
3
4
5
6
7
8
9
10
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<StackPanel>
<TextBlock FontWeight="Bold" FontSize="40pt" Foreground="Blue">
Hello World!
</TextBlock>
<Button x:Name="MyButton">MyCaption</Button>
</StackPanel>
</Window>


Here's a function to instantiate a Window from the above (or any other Xaml file defining a Window):

1
2
3
4
5
6
7
8
9
10
open System.IO
open System.Windows
open System.Windows.Markup
 
let CreateWindowFromXAMLFile (filename : string) = 
    use l_stream = new FileStream(filename, FileMode.Open, FileAccess.Read)
    let l_window = XamlReader.Load(l_stream) :?> Window
    l_window.Height <- 500.0
    l_window.Width <- 500.0
    l_window


Here's how to call the above and hook up an event handler on the button named "MyButton" defined in the XAML, assuming the xaml-file is named "default.xaml" (and lives in the same directory as the executable):

1
2
3
4
5
6
let l_window = CreateWindowFromXAMLFile("default.xaml")
let myButton = l_window.FindName("MyButton") :?> Button
myButton.Click.Add (fun _ ->
    myButton.Content <- "Clicked!"
)
l_window.Show()
By on 12/21/2008 4:43 PM ()

Thank you, that's interesting.

Using findname("") plus a downcast to refer to controls statically defined in the xaml file is not something you need to do in C# or Visual Basic.

By on 12/22/2008 12:00 AM ()

I don't think that's quite true, I believe you do, just it's hidden from you by the designer generated code.

Also you are not forced to use XAML you can create the controls objects directly in F# code, then you will already hold references to them.

Cheers,

Rob

By on 12/22/2008 2:28 AM ()

Check out Steve Gilham's blog ---

Distributed Memory: F# CTP and Silverlight 2 and XAML

XAML integration in the F# plus Silverlight combination ...

Data binding sort of works sometimes; I can get it to work when set at run-time to bind a string to a TextBlock via

1
myTextBlock.DataContext <- this.StringProperty

(but not yet through XAML), and Sliders are being totally recalcitrant...

I think there is some autogeneration magic just not happening for F# -- the best I can get seems to be to approximate a one-time binding -- so it's back to wiring up events by hand.

By on 12/30/2008 9:06 AM ()
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