{"record":{"id":"37ff9bdc74f4ade8","repo":"PrismLibrary/Prism","slug":"resources-canonlyaddtypesthatinheritifromregionbehavior","errorCode":null,"errorMessage":"Resources.CanOnlyAddTypesThatInheritIFromRegionBehavior (formatted with behaviorType.Name)","messagePattern":"Resources\\.CanOnlyAddTypesThatInheritIFromRegionBehavior \\(formatted with behaviorType\\.Name\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Prism.Core/Navigation/Regions/RegionBehaviorFactory.cs","lineNumber":48,"sourceCode":"        /// </summary>\n        /// <param name=\"behaviorKey\">The behavior key that's used to find if a certain behavior is already added.</param>\n        /// <param name=\"behaviorType\">Type of the behavior to add.</param>\n        public void AddIfMissing(string behaviorKey, Type behaviorType)\n        {\n            if (behaviorKey == null)\n            {\n                throw new ArgumentNullException(nameof(behaviorKey));\n            }\n\n            if (behaviorType == null)\n            {\n                throw new ArgumentNullException(nameof(behaviorType));\n            }\n\n            // Check if the type is a IRegionBehavior\n            if (!typeof(IRegionBehavior).IsAssignableFrom(behaviorType))\n            {\n                throw new ArgumentException(\n                    string.Format(Thread.CurrentThread.CurrentCulture, Resources.CanOnlyAddTypesThatInheritIFromRegionBehavior, behaviorType.Name), nameof(behaviorType));\n            }\n\n            // Only add the behaviorKey if it doesn't already exists.\n            if (_registeredBehaviors.ContainsKey(behaviorKey))\n            {\n                return;\n            }\n\n            _registeredBehaviors.Add(behaviorKey, behaviorType);\n        }\n\n        /// <summary>\n        /// Adds or replaces a particular type of RegionBehavior. The <paramref name=\"behaviorKey\"/> string is used to check if the behavior is already present\n        /// </summary>\n        /// <param name=\"behaviorKey\">The behavior key that's used to find if a certain behavior is already added.</param>\n        /// <param name=\"behaviorType\">Type of the behavior to add.</param>\n        public void AddOrReplace(string behaviorKey, Type behaviorType)","sourceCodeStart":30,"sourceCodeEnd":66,"githubUrl":"https://github.com/PrismLibrary/Prism/blob/358118cd640d9a22ff8cf21c8ad197fa038b7990/src/Prism.Core/Navigation/Regions/RegionBehaviorFactory.cs#L30-L66","documentation":"RegionBehaviorFactory.AddIfMissing throws ArgumentException when the supplied type does not implement IRegionBehavior. The message (Resources.CanOnlyAddTypesThatInheritIFromRegionBehavior, formatted with the type's Name) tells you which type was rejected.","triggerScenarios":"Registering a class that lacks the IRegionBehavior interface, or registering an interface/abstract type, in ConfigureDefaultRegionBehaviors.","commonSituations":"Renaming/refactoring dropped the IRegionBehavior implementation; registering a view-model or service class by mistake; generic type close errors made the type not implement the interface.","solutions":["Make the registered class implement IRegionBehavior (Region property + OnAttach)","Verify the registered type is the concrete behavior class, not a service or view-model","Confirm the type is a concrete class implementing the interface before registering"],"exampleFix":"// before\nfactory.AddIfMissing(\"MyKey\", typeof(MyService)); // not IRegionBehavior\n// after\nclass MyBehavior : IRegionBehavior { ... }\nfactory.AddIfMissing(\"MyKey\", typeof(MyBehavior));","handlingStrategy":"validation","validationCode":"if (!typeof(IRegionBehavior).IsAssignableFrom(behaviorType)) throw new ArgumentException($\"{behaviorType.Name} must implement IRegionBehavior\");","typeGuard":"bool IsRegionBehavior(Type t) => typeof(IRegionBehavior).IsAssignableFrom(t);","tryCatchPattern":"try { factory.AddIfMissing(key, t); } catch (ArgumentException ex) when (ex.ParamName == \"behaviorType\") { /* register a real IRegionBehavior */ }","preventionTips":["Ensure behavior classes implement IRegionBehavior","Only register concrete behavior types","Add a startup unit test asserting all registered types implement IRegionBehavior"],"tags":["prism","regions","type-mismatch","factory"],"backgroundTag":"type-mismatch","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"}