PrismLibrary/Prism · error · ArgumentException

Resources.CanOnlyAddTypesThatInheritIFromRegionBehavior…

Error message

Resources.CanOnlyAddTypesThatInheritIFromRegionBehavior (formatted with behaviorType.Name)

What it means

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.

Solutions

  1. Make the registered class implement IRegionBehavior (Region property + OnAttach)
  2. Verify the registered type is the concrete behavior class, not a service or view-model
  3. Confirm the type is a concrete class implementing the interface before registering

Example fix

// before
factory.AddIfMissing("MyKey", typeof(MyService)); // not IRegionBehavior
// after
class MyBehavior : IRegionBehavior { ... }
factory.AddIfMissing("MyKey", typeof(MyBehavior));
Defensive patterns

Strategy: validation

Validate before calling

if (!typeof(IRegionBehavior).IsAssignableFrom(behaviorType)) throw new ArgumentException($"{behaviorType.Name} must implement IRegionBehavior");

Type guard

bool IsRegionBehavior(Type t) => typeof(IRegionBehavior).IsAssignableFrom(t);

Try / catch

try { factory.AddIfMissing(key, t); } catch (ArgumentException ex) when (ex.ParamName == "behaviorType") { /* register a real IRegionBehavior */ }

Prevention

When it happens

Trigger: Registering a class that lacks the IRegionBehavior interface, or registering an interface/abstract type, in ConfigureDefaultRegionBehaviors.

Common situations: 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.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


AI-assisted analysis of PrismLibrary/Prism@358118cd64 (2026-09-15). Data as JSON: /api/errors/37ff9bdc74f4ade8. Report an issue: GitHub.

Appendix: source

Thrown at src/Prism.Core/Navigation/Regions/RegionBehaviorFactory.cs:48

        /// </summary>
        /// <param name="behaviorKey">The behavior key that's used to find if a certain behavior is already added.</param>
        /// <param name="behaviorType">Type of the behavior to add.</param>
        public void AddIfMissing(string behaviorKey, Type behaviorType)
        {
            if (behaviorKey == null)
            {
                throw new ArgumentNullException(nameof(behaviorKey));
            }

            if (behaviorType == null)
            {
                throw new ArgumentNullException(nameof(behaviorType));
            }

            // Check if the type is a IRegionBehavior
            if (!typeof(IRegionBehavior).IsAssignableFrom(behaviorType))
            {
                throw new ArgumentException(
                    string.Format(Thread.CurrentThread.CurrentCulture, Resources.CanOnlyAddTypesThatInheritIFromRegionBehavior, behaviorType.Name), nameof(behaviorType));
            }

            // Only add the behaviorKey if it doesn't already exists.
            if (_registeredBehaviors.ContainsKey(behaviorKey))
            {
                return;
            }

            _registeredBehaviors.Add(behaviorKey, behaviorType);
        }

        /// <summary>
        /// Adds or replaces a particular type of RegionBehavior. The <paramref name="behaviorKey"/> string is used to check if the behavior is already present
        /// </summary>
        /// <param name="behaviorKey">The behavior key that's used to find if a certain behavior is already added.</param>
        /// <param name="behaviorType">Type of the behavior to add.</param>
        public void AddOrReplace(string behaviorKey, Type behaviorType)

View on GitHub (pinned to 358118cd64)