回购https://github.com/francotiveron/WpfApp1包含以下项目:

  1. C#WPF库(WpfLibrary1)
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net6.0-windows</TargetFramework>
    <Nullable>enable</Nullable>
    <UseWPF>true</UseWPF>
  </PropertyGroup>

</Project>
using System;
using System.Windows.Input;

namespace WpfLibrary1
{
    public static class CustomCommands
    {
        public static readonly RoutedCommand DifferentAssemblyCommand = new RoutedCommand("DifferentAssemblyCommand", typeof(CustomCommands));
    }
}

  1. 一个F#库(FsLibrary1)
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
      <TargetFramework>net6.0-windows</TargetFramework>
      <UseWPF>true</UseWPF>
  </PropertyGroup>

  <ItemGroup>
    <Compile Include="Library.fs" />
  </ItemGroup>

</Project>
namespace FsLibrary1

open System.Windows.Input

type CustomCommands =
    static member FsAssemblyCommand = RoutedCommand("OpenChartCommand", typeof<CustomCommands>)
  1. 引用%1和%2的WPF应用程序(WpfApp3).
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net6.0-windows</TargetFramework>
    <Nullable>enable</Nullable>
    <UseWPF>true</UseWPF>
  </PropertyGroup>

  <ItemGroup>
    <ProjectReference Include="..\FsLibrary1\FsLibrary1.fsproj" />
    <ProjectReference Include="..\WpfLibrary1\WpfLibrary1.csproj" />
  </ItemGroup>

</Project>

<Window x:Class="WpfApp3.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp3"
        xmlns:lib="clr-namespace:WpfLibrary1;assembly=WpfLibrary1"
        xmlns:fs="clr-namespace:FsLibrary1;assembly=FsLibrary1"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Button Content="SameAssemblyCommand" Command="local:CustomCommands.SameAssemblyCommand" HorizontalAlignment="Left" Margin="280,171,0,0" VerticalAlignment="Top" RenderTransformOrigin="-2.909,-2.677"/>
        <Button Content="DifferentAssemblyCommand" Command="lib:CustomCommands.DifferentAssemblyCommand" HorizontalAlignment="Left" Margin="280,230,0,0" VerticalAlignment="Top" RenderTransformOrigin="-2.909,-2.677"/>
        <Button Content="FsAssemblyCommand" Command="fs:CustomCommands.FsAssemblyCommand" HorizontalAlignment="Left" Margin="280,292,0,0" VerticalAlignment="Top" RenderTransformOrigin="-2.909,-2.677"/>

    </Grid>
</Window>

namespace WpfApp3
{
    public static class CustomCommands
    {
        public static readonly RoutedCommand SameAssemblyCommand = new RoutedCommand("SameAssemblyCommand", typeof(CustomCommands));
    }
    public partial class MainWindow : Window
    {

        public MainWindow()
        {
            InitializeComponent();
            var cmdBindings = new CommandBinding(CustomCommands.SameAssemblyCommand);
            cmdBindings.Executed += SameAssemblyCommandExecuted;
            //cmdBindings.CanExecute += OpenChartCanExecute1;
            CommandBindings.Add(cmdBindings);
            
            cmdBindings = new CommandBinding(WpfLibrary1.CustomCommands.DifferentAssemblyCommand);
            cmdBindings.Executed += DifferentAssemblyCommandExecuted;
            //cmdBindings.CanExecute += OpenChartCanExecute1;
            CommandBindings.Add(cmdBindings);

            cmdBindings = new CommandBinding(FsLibrary1.CustomCommands.FsAssemblyCommand);
            cmdBindings.Executed += FsAssemblyCommandExecuted;
            //cmdBindings.CanExecute += OpenChartCanExecute1;
            CommandBindings.Add(cmdBindings);
        }

        private void FsAssemblyCommandExecuted(object sender, ExecutedRoutedEventArgs e) { }
        private void SameAssemblyCommandExecuted(object sender, ExecutedRoutedEventArgs e) { }
        private void DifferentAssemblyCommandExecuted(object sender, ExecutedRoutedEventArgs e) { }
    }
}

绑定到应用程序程序集中和C#库中的命令的工作按预期进行.相反,来自F#库的CommandBinding不起作用.

How can I make the F# CommandBinding work?

解 我不认为F#语法会产生一个属性getter而不是一个类字段.

enter image description here

推荐答案

问题是,您的F#代码在每次调用FsAssemblyCommand时都会创建一个新的RoutedCommand.因此,在元素树中永远找不到相应的命令绑定.

如果希望该成员的行为类似于只初始化一次的C#字段,则可以改用member val:

type CustomCommands() =
    static member val FsAssemblyCommand = RoutedCommand("OpenChartCommand", typeof<CustomCommands>)

或者,您可以创建一个let绑定值,然后每次都返回该值:

type CustomCommands() =
    static let cmd = RoutedCommand("OpenChartCommand", typeof<CustomCommands>)
    static member FsAssemblyCommand = cmd

Csharp相关问答推荐

更新数据库中的对象失败,原因是:Microsoft. EntityFrame Core. GbUpdateConcurrencyResponse'

ListaryImportProperty的默认DllImportSearchPathsProperty行为

更改对象的旋转方向

解析需要HttpClient和字符串的服务

静态对象构造顺序

在实体框架中处理通用实体&S变更跟踪器

如何使用MailKit删除邮箱?

如何在CSharp中将json字符串转换为DataTable?

使用动态键从请求体反序列化JSON

如何使用Npgsql从SELECT获得所有查询结果

如何在一次数据库调用中为ASP.NET核心身份用户加载角色

使用可空引用类型时C#接口实现错误

如何读取TagHelper属性的文本值?

如何保存具有多个重叠图片框的图片框?

如何对正方形格线进行对角分组

与Visual Studio 2022中的.NET框架相比,如何在.NET Core 6中获取错误输出的窗口句柄

无法将.Net Framework 4.8.1升级到.Net 7

身份验证中间件如何处理多个方案

LINQ在GROUP BY和JOIN之后获取子列表

如何提高C#中比较大 struct 的性能?