PrismLibrary/Prism · error · ArgumentException

Resources.RegionManagerWithDifferentNameException…

Error message

Resources.RegionManagerWithDifferentNameException (formatted with region.Name, regionName)

What it means

RegionCollection.Add(string regionName, IRegion region) throws ArgumentException (parameter regionName) when the region already has a non-null Name that differs from the regionName argument. The two must match; otherwise the registration is ambiguous.

Solutions

  1. Pass the regionName that matches region.Name exactly.
  2. Set region.Name = null before adding under a new name (Add will then assign regionName).
  3. Use the single-argument Add(region) overload if the region's existing name is authoritative.

Example fix

// before
region.Name = "ContentRegion";
regionManager.Regions.Add("MainRegion", region); // name mismatch
// after
regionManager.Regions.Add("ContentRegion", region);
Defensive patterns

Strategy: validation

Validate before calling

if (!string.Equals(region?.Name, regionName) && region?.Name != null)
    throw new InvalidOperationException($"Region name '{region.Name}' does not match '{regionName}'");
regionManager.Regions.Add(regionName, region);

Type guard

bool NamesMatch(IRegion r, string name) => r?.Name is null || r.Name == name;

Try / catch

try { regionManager.Regions.Add(regionName, region); }
catch (ArgumentException ex) { Console.WriteLine($"Name mismatch: {ex.Message}"); }

Prevention

When it happens

Trigger: Calling Regions.Add("A", region) where region.Name is set and equals something other than "A" (e.g. region.Name == "B").

Common situations: Registering a region under an alias or renamed key while the region instance already carries its original name; mismatched string literals between region creation and registration; refactors renaming one side but not the other.

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

Appendix: source

Thrown at src/Maui/Prism.Maui/Navigation/Regions/RegionCollection.cs:110

        Xaml.RegionManager.UpdateRegions();

        return GetRegionByName(regionName) != null;
    }

    /// <summary>
    /// Adds a region to the <see cref="RegionManager"/> with the name received as argument.
    /// </summary>
    /// <param name="regionName">The name to be given to the region.</param>
    /// <param name="region">The region to be added to the <see cref="RegionManager"/>.</param>
    /// <exception cref="ArgumentNullException">Thrown if <paramref name="region"/> is <see langword="null"/>.</exception>
    /// <exception cref="ArgumentException">Thrown if <paramref name="regionName"/> and <paramref name="region"/>'s name do not match and the <paramref name="region"/> <see cref="IRegion.Name"/> is not <see langword="null"/>.</exception>
    public void Add(string regionName, IRegion region)
    {
        if (region == null)
            throw new ArgumentNullException(nameof(region));

        if (region.Name != null && region.Name != regionName)
            throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Resources.RegionManagerWithDifferentNameException, region.Name, regionName), nameof(regionName));

        if (region.Name == null)
            region.Name = regionName;

        Add(region);
    }

    private IRegion GetRegionByName(string regionName) =>
        _regions.FirstOrDefault(r => r.Name == regionName);

    private void OnCollectionChanged(NotifyCollectionChangedEventArgs notifyCollectionChangedEventArgs) =>
        CollectionChanged?.Invoke(this, notifyCollectionChangedEventArgs);
}

View on GitHub (pinned to 358118cd64)