Some things didn't work out but they were only minor.
Things like DialogResult and ShowModalDialog I couldn't get to work.
Also, setting IsCancel="true" in the xaml rather than binding a command didn't seem to work out either. I think this has more to do with the fact that the xaml is a partial class to the view model (under the hood) and we can't have a partial class in F#, I tried everything I could but what I have now works.
I also added some code that loads the xaml as a resource at runtime rather than having to load it from text and compile it.
You will need to set the 'Build Action' property of the .xaml file to 'Resource'
Thanks for the help!
Here's the main changes I made to the ViewModel class:
let resultOk param =
let win = unbox param
win.Hide()
drawLine(layer)
win.Show()
let resultCancel param =
(unbox param).Close()
And here's how I loaded the xaml resource:
(*--- Helper Functions for wpf ---*)
module FsWpfHelpers =
let (?) (this : Control) (prop : string) : 'T =
this.FindName(prop) :?> 'T
let LoadXaml(appName : string, xamlFileName : string) =
let uri = @"/" + appName + ";component/" + xamlFileName + ""
Application.LoadComponent(
new Uri(uri, UriKind.Relative)) :?> 'a
open FsWpfHelpers
type CommandMethods() =
[]
member x.Test () =
let win = LoadXaml("WpfDialogSample", "Window1.xaml")
win.Show()
[)>]
do ()
And the xaml:
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:ViewModel="clr-namespace:WpfDialogSample;assembly=WpfDialogSample"
Name="mainWindow"
Title="Draw Line"
WindowStartupLocation="CenterOwner" Height="120" Width="280" ResizeMode="NoResize">
ItemsSource="{Binding Layers}" SelectedItem="{Binding Layer}"/>
Command="{Binding OkCommand}" CommandParameter="{Binding ElementName=mainWindow}"/>
Command="{Binding CancelCommand}" CommandParameter="{Binding ElementName=mainWindow}"/>
Interesting.Another reason to delay my exploration of F#. true, it can be frustrating but it will only be a matter of time until F# tools catch up.
My interest in F# comes from my frustrations writing boiler plate code all the time in C#, F# is much more concise and easier to read (once you get over what all the 'let's are about).
Every time I write an if statement or for loop it drives me nuts and learning a functional language in my spare time has been very rewarding.
Ideally I'd use a Lisp but the similarities between F# and a Lisp are close enough and I've grown to like the pipe operator in preference to digging into paren's to see what's going on.
I love the way you can build very simple building block functions and string them together to build more interesting ones.
Having said that though, from learning a functional language I now have a much better understanding of Linq and Lambdas, the dangers/problems of immutability and how to code in a more expressive style in C#. Any C# I write/edit from now on will be a lot better (than what I used to write at least).
Even C++ (C++11 on) is getting more functional to cope with the onslaught of multi-core processors.
OO has dug itself into a pretty big hole and only by introducing more functional paradigms (such as immutability and higher order functions) will old code bases be able to be refactored to take advantage of new hardware technology.
It's not just hardware on a single machine either, think passing portions of your calculations to multi pc's in the cloud.
Here's an interesting article worth a read by Joel Spolsky (written nearly 10 years ago!) that explains a lot better my interest in this field
http://www.joelonsoftware.com/items/2006/08/01.html
No wonder people coming from lisp have a hard time learning OO, they took the 'telling' the program what to do into 'describing' what to do (i.e. expressions vs statements). I thought F# was not used much in actual "designer-tooling code" UI and would use C# if not a as*hole and VB if a di*k, but the libraries used to dynamically build UI, creating, transforming, etc.. data would be F#.
I remember one guy who gave a interview for .NET Rocks worked for some kind casino type app that was very UI oriented but were using C# and would create new game about every week and F# cut it down to about a day. I think C# was still used in designer code of UI but F# libraries used to create UI controls, components, etc....
页:
1
[2]