F#: is there no UI (like WPF) for it?

后端 未结 7 940
梦毁少年i
梦毁少年i 2021-01-30 18:41

i recently saw some videos on F#. it seems it used mainly for Service or Classes only? i see no \"F# WPF\" app in VS2010 Beta?

相关标签:
7条回答
  • 2021-01-30 18:45

    It is a .NET language, so it can use the .NET class library. Which means Winforms, WPF or anything else you might use in C#.

    0 讨论(0)
  • 2021-01-30 18:48

    You can certainly create GUIs in F# - it's just another .NET language, after all.

    Tomas Petricek's book, Functional Programming for the Real World (which I've helped out with a little bit) has various GUI examples. The source code is available to download if you want to see examples.

    Admittedly some aspects of GUI programming don't map terribly well to a functional style, as there's a lot of mutation involved, but there are ways and means around that :)

    0 讨论(0)
  • 2021-01-30 18:50

    We sell a commercial library called F# for Visualization that is written in 100% F# code and uses WPF to provide interactive graphics with typeset mathematics from your F# code:


    (source: ffconsultancy.com)

    So it is certainly possible to write GUI apps in F# using WPF.

    0 讨论(0)
  • 2021-01-30 18:55

    Of course you can use all the WPF classes from F#, too. You can create Windows, controls and everything else from F# and I also sometimes use it from the F# console.

    It's not as tightly integrated into Visual Studio as C# or VB yet, but as you can see in the comments, designer support in the future is feasible. I guess we'll have to wait until then (or use other tools).

    0 讨论(0)
  • 2021-01-30 19:00

    Updated according to the link by James Hugard

    For now F# will not be used much for GUIs because it is not the most important use case for it. From Don Syme's blog:

    In this first supported release, our aim has to be to focus on the core strengths of F# for exploratory programming with F# Interactive, programming with data and implementing parallel and asynchronous components.

    Although you can theoretically use F# and the standard GUI libraries if you need a GUI you should use VB or C#:

    F# users should use the Visual Studio designer tools to generate C# or Visual Basic code and incorporate those components into their F# applications.

    In the longer term "presentation-oriented designer tools that generate F# code" are, according to Syme, "definitely feasible".

    0 讨论(0)
  • 2021-01-30 19:05

    F# actually has some very nice constructs for creating event-driven UI applications, such as First Class Events, Object Expressions, calling property setters from a constructor e.g.:

    new Form(Text="My Window Title", Width=600, Height=400),

    and much else.

    However, creating a forms designer in VS reqiures a CodeDom for your language. The current CodeDom architecture works great, as long as your language looks exactly like C# or VB; it does not lend itself well to generation of F# code (this from a webcast or interview that I can't locate right now). It also requires partial classes, which if I recall correctly, are not supported in the language as of Beta 1. Rather than focus on designer support in the first release, the F# team decided to spend their resources on enhancing other parts of the language, such as asynchronous and parallel programming, etc.

    What this means is that you have at least 4 choices for creating UI in F#:

    • Write all UI code by hand, which is fine for simple apps;
    • Create your F# code as a library to handle the "hard parts," like asynchronous and parallel code, or computation centric code, and call it from C#/VB;
    • Create your UI code as a C#/VB library, and both drive it from F# and delegate event handling to F#; or
    • Use a DSL or Computation Expression (monad) to simplify building the UI by hand (just discovered this while looking for other links in this answer).

    Of these, calling a C# UI library from F# may be the most flexible while still retaining a familiar paradigm. But, using computation expressions for quickly building UI by hand is certainly worth looking at.

    0 讨论(0)
提交回复
热议问题