PrismLibrary/Prism · error · ArgumentNullException

ArgumentNullException(nameof(behaviorKey))

Error message

ArgumentNullException(nameof(behaviorKey))

What it means

Validation guard in RegionBehaviorFactory.AddIfMissing: the behaviorKey string is used both to check whether a behavior type is already registered and as the registration key; a null key cannot identify a behavior, so ArgumentNullException naming 'behaviorKey' is thrown before any registration check.

Solutions

  1. Pass a non-null key, e.g. nameof(MyBehavior)
  2. Declare the key as a const string on the behavior class
  3. Check the argument at the call site in ConfigureDefaultRegionBehaviors

Example fix

// before
factory.AddIfMissing(null, typeof(MyBehavior));
// after
factory.AddIfMissing(nameof(MyBehavior), typeof(MyBehavior));
Defensive patterns

Strategy: validation

Validate before calling

if (string.IsNullOrEmpty(key)) throw new ArgumentException("key required"); factory.AddIfMissing(key, behaviorType);

Type guard

bool ValidKey(string k) => !string.IsNullOrEmpty(k);

Try / catch

try { factory.AddIfMissing(key, t); } catch (ArgumentNullException ex) when (ex.ParamName == "behaviorKey") { /* supply key */ }

Prevention

When it happens

Trigger: Calling AddIfMissing(null, typeof(MyBehavior)) in ConfigureDefaultRegionBehaviors, typically from a null constant or nameof() on a missing member.

Common situations: Registering custom behaviors at app startup with a key built from configuration; refactoring removed the key constant.

Related errors


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

Appendix: source

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

        /// <summary>
        /// Initializes a new instance of <see cref="RegionBehaviorFactory"/>.
        /// </summary>
        /// <param name="container"><see cref="IContainerExtension"/> used to create the instance of the behavior from its <see cref="Type"/>.</param>
        public RegionBehaviorFactory(IContainerExtension container)
        {
            _container = container;
        }

        /// <summary>
        /// Adds a particular type of RegionBehavior if it was not already registered. 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 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;

View on GitHub (pinned to 358118cd64)