Search code examples
c#.net-corerelaycommand.net-core-3.0

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


My RelayCommand includes the implementation of CommandManager which is not known by .net core 3 preview 3. However, Microsoft says the it is available: see here

Dependencies

I installed/uninstalled .Net Core and restarted visual studio 2019 preview but without success. OS is 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); }

    }

Solution

  • A bit late but someone else may benefit.

    This is for targeting .NET Core 3.0 Preview 6

    I installed via NuGet the following into my WPF UI project:

    Microsoft.NETCore.Platforms Version="3.0.0-preview6.19303.8"

    Microsoft.NETCore.Targets Version="3.0.0-preview6.19303.8"

    Microsoft.WindowsDesktop.App Version="3.0.0-preview6-27804-01"

    System.Windows.Input.CommandManager is then available.

    You should already have SDK namely Microsoft.NETCore.App (3.0.0-preview6-27804-01)