PrismLibrary/Prism · error · ArgumentException

Resources.TypeWithKeyNotRegistered (formatted with key)

Error message

Resources.TypeWithKeyNotRegistered (formatted with key)

What it means

RegionBehaviorFactory.CreateFromKey throws ArgumentException with the resource string Resources.TypeWithKeyNotRegistered when the requested behavior key was never registered with Attach. The factory can only create behaviors it knows about via its registration dictionary, so an unknown key is a caller bug.

Solutions

  1. Ensure Attach(key, behaviorType) was called for the exact key before CreateFromKey
  2. Check key spelling/casing against the registered behavior key constants
  3. If the behavior may not exist, call ContainsKey(key) first
  4. Update keys after a Prism upgrade where built-in behavior keys changed

Example fix

// before
var b = factory.CreateFromKey("MyBehvior");
// after
var key = "MyBehavior";
if (!factory.ContainsKey(key)) factory.Attach(key, typeof(MyBehavior));
var b = factory.CreateFromKey(key);
Defensive patterns

Strategy: validation

Validate before calling

if (!factory.ContainsKey(key)) throw new InvalidOperationException($"Behavior '{key}' not registered; call factory.Attach first");
var behavior = factory.CreateFromKey(key);

Try / catch

try { var b = factory.CreateFromKey(key); }
catch (ArgumentException ex) when (ex.ParamName == "key") { log.Warn($"Unregistered region behavior key: {key}"); }

Prevention

When it happens

Trigger: Calling CreateFromKey with a key that was never passed to Attach (e.g. typo in the behavior key, or CreateFromKey called before Attach registration, or key removed by Detach/ContainsKey check race).

Common situations: Custom region behaviors registered under different keys between Prism versions; copy-pasted factory code referencing old behavior keys; unit tests (ExistingBehavior_IsReplaced_WithCustomBehavior, MissingBehavior_IsAdded) exercising unregistered keys.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


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

Appendix: source

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

            if (_registeredBehaviors.ContainsKey(behaviorKey)
                && _registeredBehaviors[behaviorKey].Equals(behaviorType) == false)
            {
                _registeredBehaviors.Remove(behaviorKey);
            }

            AddIfMissing(behaviorKey, behaviorType);
        }

        /// <summary>
        /// Creates an instance of the behavior <see cref="Type"/> that is registered using the specified key.
        /// </summary>
        /// <param name="key">The key that is used to register a behavior type.</param>
        /// <returns>A new instance of the behavior. </returns>
        public IRegionBehavior CreateFromKey(string key)
        {
            if (!ContainsKey(key))
            {
                throw new ArgumentException(
                    string.Format(Thread.CurrentThread.CurrentCulture, Resources.TypeWithKeyNotRegistered, key), nameof(key));
            }

            return (IRegionBehavior)_container.Resolve(_registeredBehaviors[key]);
        }


        /// <summary>
        /// Returns an enumerator that iterates through the collection.
        /// </summary>
        /// <returns>
        /// A <see cref="T:System.Collections.Generic.IEnumerator`1" /> that can be used to iterate through the collection.
        /// </returns>
        /// <filterpriority>1</filterpriority>
        public IEnumerator<string> GetEnumerator()
        {
            return _registeredBehaviors.Keys.GetEnumerator();
        }

View on GitHub (pinned to 358118cd64)