。net core 3.1将CommandManager-类插入到类库项目中

问题描述 投票:0回答:1

我目前正在尝试将我几年前创建的MVVM库从.NET 4.5迁移到.NET Core 3.1。效果出奇的好,但是此刻我正在与我在RelayCommand-Class中使用的CommandManager-Class挣扎。

我将CommandManager用于我的RelayCommand-Class的CanExecute事件处理程序:

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不属于.NET Core。有很多解决方案建议将Projecttarget从Classlibrary切换到WPF-Application或嵌入PresentationCore-Assembly。

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

所以我想问一下它们是否在.NET Core内部存在类似的类?还是尝试编写自己的CommandManager-Class来代替它会更好?

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

亲切的问候

GeoCoder

c# .net .net-core class-library
1个回答
0
投票

.csproj文件更改为此:

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

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

</Project>
© www.soinside.com 2019 - 2024. All rights reserved.