乐筑天下

搜索
欢迎各位开发者和用户入驻本平台 尊重版权,从我做起,拒绝盗版,拒绝倒卖 签到、发布资源、邀请好友注册,可以获得银币 请注意保管好自己的密码,避免账户资金被盗
楼主: MickD

F# - Use WPF dialog button to draw a line

[复制链接]

69

主题

875

帖子

15

银币

顶梁支柱

Rank: 50Rank: 50

铜币
1146
发表于 2015-5-30 16:10:49 | 显示全部楼层
Nice suggestions, thanks kaefer.
回复

使用道具 举报

24

主题

204

帖子

6

银币

后起之秀

Rank: 20Rank: 20Rank: 20Rank: 20

铜币
300
发表于 2015-8-14 03:33:47 | 显示全部楼层
It's been a while since I've had time to play but I've strung most of what you guys have here into something that works ok.
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:
[ol]    let resultOk param =
        let win = unbox param
        win.Hide()
        drawLine(layer)
        win.Show()

    let resultCancel param =
        (unbox param).Close()
[/ol]
And here's how I loaded the xaml resource:
[ol](*--- 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 ()
[/ol]
And the xaml:
[ol]
        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}"/>
            
            
                [B]
                        Command="{Binding OkCommand}" CommandParameter="{Binding ElementName=mainWindow}"/>
                [B]
                        Command="{Binding CancelCommand}" CommandParameter="{Binding ElementName=mainWindow}"/>
            
        
   
[/ol]
回复

使用道具 举报

69

主题

875

帖子

15

银币

顶梁支柱

Rank: 50Rank: 50

铜币
1146
发表于 2015-8-14 09:48:01 | 显示全部楼层

Interesting.  Another reason to delay my exploration of F#.
回复

使用道具 举报

15

主题

687

帖子

169

银币

中流砥柱

Rank: 25

铜币
582
发表于 2015-8-14 19:38:47 | 显示全部楼层
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).
回复

使用道具 举报

69

主题

875

帖子

15

银币

顶梁支柱

Rank: 50Rank: 50

铜币
1146
发表于 2015-8-15 00:25:00 | 显示全部楼层
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....
回复

使用道具 举报

发表回复

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

QQ|关于我们|小黑屋|乐筑天下 繁体中文

GMT+8, 2024-11-22 01:32 , Processed in 0.141930 second(s), 60 queries .

© 2020-2024 乐筑天下

联系客服 关注微信 帮助中心 下载APP 返回顶部 返回列表