Create F# WPF Code Behind Application

Create an F# console application (.NET Framework).

Change the Output type of the application to Windows Application.

Add the FsXaml.Wpf NuGet package.

Add these four source files, in the order listed here.

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.

Change the Build Action to Resource for the two xaml files.

Add a reference to the .NET assembly UIAutomationTypes.

项目文件(.fsproj):

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net481</TargetFramework> 
    <WarnOn>3390;$(WarnOn)</WarnOn>
  </PropertyGroup>

  <ItemGroup>
    <Resource Include="App.xaml" />
    <Resource Include="MainWindow.xaml" />
    <Compile Include="MainWindow.xaml.fs" />
    <Compile Include="App.fs" />
  </ItemGroup>

  <ItemGroup>
    <PackageReference Include="FsXaml.Wpf" Version="3.1.6" />
    <Reference Include="UIAutomationTypes" />
  </ItemGroup>
</Project>

Compile and run.

You can't use the designer to add event handlers, but that's not a problem at all. Simply add them manually in the code behind, like you see with the three handlers in this example, including the handler for the test button.

已知问题:

  • 如果不设置为资源,alt上下调整文件顺序可能会被禁用。

  • XAML设计器有一些小的控制按钮,交换视图,隐藏视图。

引用当前程序集的UserControl时,

<mah:MetroWindow
    xmlns:local="clr-namespace:Translator"
>
<local:SentanceUserControl 

运行时异常:

XamlObjectWriterException: 无法创建未知类型“{clr-namespace:Translator}SentanceUserControl”。

解决方案:

This is required because when App.exe is running, it may not have loaded the controls library yet. 程序找不到类型定义,你需要提供assembly:

<mah:MetroWindow
    xmlns:local="clr-namespace:Translator;assembly=Translator"
>

微软文档

  • net481可以引用netstandard2.0库。