将 RelayCommand (CommandManager) 从 .Net Framework 移动到 .NET Core v3.0.100-preview3

Moving RelayCommand (CommandManager) from .Net Framework to .NET Core v3.0.100-preview3

我的 RelayCommand 包括 .net core 3 预览版 3 不知道的 CommandManager 的实现。但是,Microsoft 说它可用:see here

我 installed/uninstalled .Net Core 并重新启动 visual studio 2019 预览版但没有成功。 OS 是 Windows 10 x64。

    public class RelayCommand : ICommand
        {

            readonly Action<object> _execute;
            readonly Predicate<object> _canExecute;


            public RelayCommand(Action<object> execute) : this(execute, null) { }
            public RelayCommand(Action<object> execute, Predicate<object> canExecute)
            {
                _execute = execute ?? throw new 
                ArgumentNullException("execute"); _canExecute = canExecute;
            }

            [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); }

    }

有点晚了,但其他人可能会受益。

这是针对 .NET Core 3.0 预览版 6

我通过 NuGet 将以下内容安装到我的 WPF UI 项目中:

Microsoft.NETCore.Platforms 版本="3.0.0-preview6.19303.8"

Microsoft.NETCore.Targets 版本="3.0.0-preview6.19303.8"

Microsoft.WindowsDesktop.App 版本="3.0.0-preview6-27804-01"

System.Windows.Input.CommandManager 可用。

你应该已经有 SDK 即 Microsoft.NETCore.App (3.0.0-preview6-27804-01)

我在我的 WPF 应用程序中使用了一个 Class 库来执行命令,但是 CommandManager 不在那里,即使文档说明它在 .NET Core 中并且它被放置在 System.Windows.Input .

多亏了 Col,我才能够通过将输出类型从 Class 库更改为 Windows 应用程序来让它工作:

右键单击您的项目 > 属性 > 应用程序