.net core 3.1 将 CommandManager-class 插入 class library-project

.net core 3.1 inserting CommandManager-class into class library-project

我目前正在尝试将我几年前创建的 MVVM 库从 .NET 4.5 迁移到 .NET Core 3.1。 这出奇地好,但目前我正在努力使用我在 RelayCommand-Class 中使用的 CommandManager-Class。

我正在为 RelayCommand 的 CanExecute 事件处理程序使用 CommandManager-Class:

public class RelayCommand : ICommand
{
    #region Properties
    readonly Action<object> _execute;
    readonly Predicate<object> _canExecute;
    #endregion

    #region Constructors

    public RelayCommand(Action<object> execute) : this(execute, null)
    {

    }

    public RelayCommand(Action<object> execute, Predicate<object> canExecute)
    {
        if (execute == null)
            throw new ArgumentNullException("execute");

        _execute = execute;
        _canExecute = canExecute;
    }
    #endregion

    #region ICommand Members

    [DebuggerStepThrough]
    public bool CanExecute(object parameter)
    {
        return _canExecute == null ? true : _canExecute(parameter);
    }

    public event EventHandler CanExecuteChanged
    {
        add { CommandManager.RequerySuggested += value; }
        remove { CommandManager.RequerySuggested -= value; }
    }

    public void Execute(object parameter)
    {
        _execute(parameter);
    }

    #endregion
}

在我研究那个问题的过程中,我发现 System.Windows.Input 不是 of.NET 核心的一部分。有许多解决方案建议将 Projecttarget 从 Classlibrary 切换到 WPF-Application 或嵌入 PresentationCore-Assembly。

这些解决方案对我不起作用 - 我猜主要是因为我使用了普通的 .NET Core Class库项目。

所以我想问一下它们是否与 .NET Core 中存在类似的 class? 或者如果我尝试编写自己的 CommandManager-Class 来替换它会更好吗?

目前最后的选择是从我的库中提取命令部分并将其直接放入使用该库的项目(avalonia 客户端应用程序)。 但这感觉不对...

亲切的问候

地理编码器

将您的 .csproj 文件更改为:

<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">

  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <UseWPF>true</UseWPF>
  </PropertyGroup>

</Project>