Catel.MVVM.CommandManager.RegisterCommand C# (CSharp) Method

RegisterCommand() public method

Registers a command with the specified command name.
The is null or whitespace. The is null. The specified command is not created using the method.
public RegisterCommand ( string commandName, ICommand command, IViewModel viewModel = null ) : void
commandName string Name of the command.
command ICommand The command.
viewModel IViewModel The view model.
return void
        public void RegisterCommand(string commandName, ICommand command, IViewModel viewModel = null)
        {
            Argument.IsNotNullOrWhitespace("commandName", commandName);
            Argument.IsNotNull("command", command);

            if (CatelEnvironment.IsInDesignMode)
            {
                return;
            }

            lock (_lockObject)
            {
                Log.Debug("Registering command to '{0}'", commandName);

                if (!_commands.ContainsKey(commandName))
                {
                    throw Log.ErrorAndCreateException<InvalidOperationException>("Command '{0}' is not yet created using the CreateCommand method", commandName);
                }

                _commands[commandName].RegisterCommand(command, viewModel);

                InvalidateCommands();
            }
        }

Usage Example

            public void ThrowsArgumentNullExceptionForNullCommand()
            {
                var vm = new CompositeCommandViewModel();
                var commandManager = new CommandManager();

                ExceptionTester.CallMethodAndExpectException<ArgumentException>(() => commandManager.RegisterCommand("MyCommand", null));
            }
All Usage Examples Of Catel.MVVM.CommandManager::RegisterCommand