在應用程式後面建立一個新的 F WPF 程式碼
建立一個 F#控制檯應用程式。
將應用程式的輸出型別更改為 Windows 應用程式。
新增 FsXaml NuGet 包。
按此處列出的順序新增這四個原始檔。
MainWindow.xaml
<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="First Demo" Height="200" Width="300">
    <Canvas>
        <Button Name="btnTest" Content="Test" Canvas.Left="10" Canvas.Top="10" Height="28" Width="72"/>
    </Canvas>
</Window>
MainWindow.xaml.fs
namespace FirstDemo
type MainWindowXaml = FsXaml.XAML<"MainWindow.xaml">
type MainWindow() as this =
    inherit MainWindowXaml()
    let whenLoaded _ =
        ()
    let whenClosing _ =
        ()
    let whenClosed _ =
        ()
    let btnTestClick _ =
        this.Title <- "Yup, it works!"
    do
        this.Loaded.Add whenLoaded
        this.Closing.Add whenClosing
        this.Closed.Add whenClosed
        this.btnTest.Click.Add btnTestClick
App.xaml 中
<Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Application.Resources>
    </Application.Resources>
</Application>
App.xaml.fs
namespace FirstDemo
open System
type App = FsXaml.XAML<"App.xaml">
module Main =
    [<STAThread; EntryPoint>]
    let main _ =
        let app = App()
        let mainWindow = new MainWindow()
        app.Run(mainWindow) // Returns application's exit code.
從專案中刪除 Program.fs 檔案。
更改生成操作,以資源為兩個 XAML 檔案。
新增對 .NET 程式集 UIAutomationTypes 的引用。
編譯並執行。
你無法使用設計器新增事件處理程式,但這根本不是問題。只需在後面的程式碼中手動新增它們,就像在本例中看到的三個處理程式一樣,包括測試按鈕的處理程式。
更新:FsXaml 中新增了另一種可能更加優雅的新增事件處理程式的方法。你可以在 XAML 中新增事件處理程式,與 C#中相同,但你必須手動執行,然後覆蓋在 F#型別中出現的相應成員。我推薦這個。