Events not working in WPF
So, I decided to try the F # Empty Windows App (WPF) link here . It has a code template like this.
module MainApp open System open System.Windows open System.Windows.Controls open FSharpx type MainWindow = XAML<"MainWindow.xaml"> let loadWindow() = let window = MainWindow() let x = new TestWindow1.Test1() x.Root.Show(); window.Root [<STAThread>] (new Application()).Run(loadWindow()) |> ignore
The window seemed to load very easily, so I assumed this is the correct way to load XAML using F #. I put TestWindow1.Test1 obviously.
So when I got to my own class (TestWindow1.Test) I got a problem. All the click events I set in the loadwindow () method didn't work.
It doesn't represent anything because of his test, but it doesn't work and I'm not sure what is wrong or how to fix it since everything compiles and there are no exceptions in debug mode.
module TestWindow1 //open statements here type Test1 = XAML<"Test1.xaml"> let LoadWindow() = let window = Test1() window.TextBOX.TextChanged.Add( fun _ -> window.textBlock.Text <- window.TextBox.Text) window.changecolorbtn.Click.Add( fun _-> let mutable x = window.textblock.Foreground :?> SolidColorBrush //down cast the brush to a solid color brush if x = Brushes.Red then x <- Brushes.Black else x <- Brushes.Red ) window.Root
I'm just not sure why it doesn't do anything. Anyway, I'd be happy to post the XAML for Test1 if it helps.
** Update **
I tried to put [<STAThread>] above a loadWindow () block like this, as per ethicallogics answer, it still doesn't work.
type MainWindow = XAML<"MainWindow.xaml"> [<STAThread>] let loadWindow() = let window = MainWindow() let x = new TestWindow1.Test1() x.Root.Show(); window.Root [<STAThread>] (new Application()).Run(loadWindow()) |> ignore
source to share
For a text box, it might be TextBOX
vs. TextBOX
...
For color: you assign the color to a local variable x
, try setting window.textblock.Foreground <- ...
, window.textblock.Foreground <- ...
or even window.textBlock.Foreground
(seal here too?)
If your code does compile, I would double check the XAML for components that have a name that differs only in the case.
source to share