{"record":{"id":"68360eaaa5321659","repo":"PrismLibrary/Prism","slug":"could-not-add-duplicate-behavior-with-same-key","errorCode":null,"errorMessage":"Could not add duplicate behavior with same key.","messagePattern":"Could not add duplicate behavior with same key\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Prism.Core/Navigation/Regions/RegionBehaviorCollection.cs","lineNumber":49,"sourceCode":"        /// Adds a <see cref=\"IRegionBehavior\"/> to the collection, using the specified key as an indexer.\n        /// </summary>\n        /// <param name=\"key\">The key that specifies the type of <see cref=\"IRegionBehavior\"/> that's added.</param>\n        /// <param name=\"regionBehavior\">The <see cref=\"IRegionBehavior\"/> to add.</param>\n        /// <exception cref=\"ArgumentNullException\">\n        /// Thrown is the <paramref name=\"key\"/> parameter is Null,\n        /// or if the <paramref name=\"regionBehavior\"/> parameter is Null.\n        /// </exception>\n        /// <exception cref=\"ArgumentException\">Thrown if a behavior with the specified Key parameter already exists.</exception>\n        public void Add(string key, IRegionBehavior regionBehavior)\n        {\n            if (key == null)\n                throw new ArgumentNullException(nameof(key));\n\n            if (regionBehavior == null)\n                throw new ArgumentNullException(nameof(regionBehavior));\n\n            if (behaviors.ContainsKey(key))\n                throw new ArgumentException(\"Could not add duplicate behavior with same key.\", nameof(key));\n\n            behaviors.Add(key, regionBehavior);\n            regionBehavior.Region = region;\n\n            regionBehavior.Attach();\n        }\n\n        /// <summary>\n        /// Checks if a <see cref=\"IRegionBehavior\"/> with the specified key is already present.\n        /// </summary>\n        /// <param name=\"key\">The key to use to find a particular <see cref=\"IRegionBehavior\"/>.</param>\n        /// <returns></returns>\n        public bool ContainsKey(string key) => behaviors.ContainsKey(key);\n\n        /// <summary>\n        /// Returns an enumerator that iterates through the collection.\n        /// </summary>\n        /// <returns>","sourceCodeStart":31,"sourceCodeEnd":67,"githubUrl":"https://github.com/PrismLibrary/Prism/blob/358118cd640d9a22ff8cf21c8ad197fa038b7990/src/Prism.Core/Navigation/Regions/RegionBehaviorCollection.cs#L31-L67","documentation":"RegionBehaviorCollection.Add throws ArgumentException when a behavior with the same key already exists in the collection. Keys are unique identifiers; region bootstrap code registers standard behaviors (e.g. RegionActiveAwareBehavior) automatically, so duplicates collide.","triggerScenarios":"Adding a behavior whose key matches one registered by Prism's default region behavior pipeline (IRegionBehaviorFactory) or added twice by your own code.","commonSituations":"Custom region adapter registering default behaviors plus your own code re-adding them; re-running registration logic on region re-creation; Prism version upgrade moved a behavior's registration into defaults.","solutions":["Check region.Behaviors.ContainsKey(key) before adding, or use RegionBehaviorFactory.AddIfMissing","Use a unique key (e.g. nameof(CustomBehavior)) that differs from Prism's built-in keys","Move registration into ConfigureDefaultRegionBehaviors so it runs once"],"exampleFix":"// before\nregion.Behaviors.Add(\"RegionActiveAwareBehavior\", new MyBehavior()); // duplicate key\n// after\nif (!region.Behaviors.ContainsKey(nameof(MyBehavior)))\n    region.Behaviors.Add(nameof(MyBehavior), new MyBehavior());","handlingStrategy":"validation","validationCode":"if (!region.Behaviors.ContainsKey(key)) region.Behaviors.Add(key, behavior);","typeGuard":null,"tryCatchPattern":"try { region.Behaviors.Add(key, b); } catch (ArgumentException) { /* behavior already registered — ignore */ }","preventionTips":["Use AddIfMissing on RegionBehaviorFactory instead of raw Add","Use unique nameof-based keys","Register default behaviors once in ConfigureDefaultRegionBehaviors"],"tags":["prism","regions","duplicate-key","collection"],"backgroundTag":"invalid-argument-value","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"}