PrismLibrary/Prism · error · ArgumentException

Could not add duplicate behavior with same key.

Error message

Could not add duplicate behavior with same key.

What it means

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.

Solutions

  1. Check region.Behaviors.ContainsKey(key) before adding, or use RegionBehaviorFactory.AddIfMissing
  2. Use a unique key (e.g. nameof(CustomBehavior)) that differs from Prism's built-in keys
  3. Move registration into ConfigureDefaultRegionBehaviors so it runs once

Example fix

// before
region.Behaviors.Add("RegionActiveAwareBehavior", new MyBehavior()); // duplicate key
// after
if (!region.Behaviors.ContainsKey(nameof(MyBehavior)))
    region.Behaviors.Add(nameof(MyBehavior), new MyBehavior());
Defensive patterns

Strategy: validation

Validate before calling

if (!region.Behaviors.ContainsKey(key)) region.Behaviors.Add(key, behavior);

Try / catch

try { region.Behaviors.Add(key, b); } catch (ArgumentException) { /* behavior already registered — ignore */ }

Prevention

When it happens

Trigger: Adding a behavior whose key matches one registered by Prism's default region behavior pipeline (IRegionBehaviorFactory) or added twice by your own code.

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

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/68360eaaa5321659. Report an issue: GitHub.

Appendix: source

Thrown at src/Prism.Core/Navigation/Regions/RegionBehaviorCollection.cs:49

        /// Adds a <see cref="IRegionBehavior"/> to the collection, using the specified key as an indexer.
        /// </summary>
        /// <param name="key">The key that specifies the type of <see cref="IRegionBehavior"/> that's added.</param>
        /// <param name="regionBehavior">The <see cref="IRegionBehavior"/> to add.</param>
        /// <exception cref="ArgumentNullException">
        /// Thrown is the <paramref name="key"/> parameter is Null,
        /// or if the <paramref name="regionBehavior"/> parameter is Null.
        /// </exception>
        /// <exception cref="ArgumentException">Thrown if a behavior with the specified Key parameter already exists.</exception>
        public void Add(string key, IRegionBehavior regionBehavior)
        {
            if (key == null)
                throw new ArgumentNullException(nameof(key));

            if (regionBehavior == null)
                throw new ArgumentNullException(nameof(regionBehavior));

            if (behaviors.ContainsKey(key))
                throw new ArgumentException("Could not add duplicate behavior with same key.", nameof(key));

            behaviors.Add(key, regionBehavior);
            regionBehavior.Region = region;

            regionBehavior.Attach();
        }

        /// <summary>
        /// Checks if a <see cref="IRegionBehavior"/> with the specified key is already present.
        /// </summary>
        /// <param name="key">The key to use to find a particular <see cref="IRegionBehavior"/>.</param>
        /// <returns></returns>
        public bool ContainsKey(string key) => behaviors.ContainsKey(key);

        /// <summary>
        /// Returns an enumerator that iterates through the collection.
        /// </summary>
        /// <returns>

View on GitHub (pinned to 358118cd64)