{"record":{"id":"8072403873c02d31","repo":"PrismLibrary/Prism","slug":"resources-cannotregistersamecommandtwice","errorCode":null,"errorMessage":"Resources.CannotRegisterSameCommandTwice","messagePattern":"Resources\\.CannotRegisterSameCommandTwice","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Prism.Core/Commands/CompositeCommand.cs","lineNumber":61,"sourceCode":"        ///  <remarks>\n        /// If this command is set to monitor command activity, and <paramref name=\"command\"/> \n        /// implements the <see cref=\"IActiveAware\"/> interface, this method will subscribe to its\n        /// <see cref=\"IActiveAware.IsActiveChanged\"/> event.\n        /// </remarks>\n        /// <param name=\"command\">The command to register.</param>\n        public virtual void RegisterCommand(ICommand command)\n        {\n            if (command == null) throw new ArgumentNullException(nameof(command));\n            if (command == this)\n            {\n                throw new ArgumentException(Resources.CannotRegisterCompositeCommandInItself);\n            }\n\n            lock (_registeredCommands)\n            {\n                if (_registeredCommands.Contains(command))\n                {\n                    throw new InvalidOperationException(Resources.CannotRegisterSameCommandTwice);\n                }\n                _registeredCommands.Add(command);\n            }\n\n            command.CanExecuteChanged += _onRegisteredCommandCanExecuteChangedHandler;\n            OnCanExecuteChanged();\n\n            if (_monitorCommandActivity)\n            {\n                if (command is IActiveAware activeAwareCommand)\n                {\n                    activeAwareCommand.IsActiveChanged += Command_IsActiveChanged;\n                }\n            }\n        }\n\n        /// <summary>\n        /// Removes a command from the collection and removes itself from the <see cref=\"ICommand.CanExecuteChanged\"/> event of it.","sourceCodeStart":43,"sourceCodeEnd":79,"githubUrl":"https://github.com/PrismLibrary/Prism/blob/358118cd640d9a22ff8cf21c8ad197fa038b7990/src/Prism.Core/Commands/CompositeCommand.cs#L43-L79","documentation":"CompositeCommand.RegisterCommand throws InvalidOperationException (Resources.CannotRegisterSameCommandTwice) if the exact command instance is already in _registeredCommands. Each child must be registered only once so Execute/CanExecute voting is not duplicated.","triggerScenarios":"Calling RegisterCommand twice with the same ICommand instance — e.g. re-running initialization code, re-subscribing on page revisit, or double construction of a ViewModel.","commonSituations":"View appearing multiple times triggering setup code that registers commands again; event handlers wired more than once; hot-reload re-executing registration.","solutions":["Guard registration with a check (e.g. composite.RegisteredCommands.Contains(cmd)).","Unregister the command before re-registering if re-initialization is intended.","Move registration to a place guaranteed to run once (constructor, not OnAppearing)."],"exampleFix":"// before\nprotected override void OnAppearing()\n{\n    _composite.RegisterCommand(SaveCommand); // runs every appearance\n}\n// after\nprotected override void OnAppearing()\n{\n    if (!_composite.RegisteredCommands.Contains(SaveCommand))\n        _composite.RegisterCommand(SaveCommand);\n}","handlingStrategy":"validation","validationCode":"if (!composite.RegisteredCommands.Contains(SaveCommand))\n    composite.RegisterCommand(SaveCommand);","typeGuard":null,"tryCatchPattern":"try { composite.RegisterCommand(cmd); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"twice\")) { /* already registered — safe to ignore */ }","preventionTips":["Register commands once in the constructor, not in OnAppearing.","Wrap registration in idempotent helpers.","Use RegisterCommand only from single-run initialization paths."],"tags":["csharp","commands","duplicate-registration"],"backgroundTag":"unsupported-operation","analyzedSha":"358118cd640d9a22ff8cf21c8ad197fa038b7990","analyzedAt":"2026-09-15T15:00:31.079Z","contentChangedAt":"2026-09-15T15:00:31.079Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}