PrismLibrary/Prism · error · InvalidOperationException

Cannot change the region name once is set. The current…

Error message

Cannot change the region name once is set. The current region name is '{0}'.

What it means

The Region.Name property throws InvalidOperationException if you attempt to change it after it has already been set to a different value. Region names are the identity key used by the RegionManager, so renaming an attached region would break region lookups. The name is effectively write-once.

Solutions

  1. Create a new Region instance for the new name instead of renaming the existing one.
  2. Keep region names constant per region lifetime; register all region names up front.
  3. Remove the old region from the RegionManager and add a new one with the desired name.

Example fix

// before
region.Name = "OtherRegion"; // throws if already named
// after
var newRegion = new Region { Name = "OtherRegion" };
regionManager.Regions.Add("OtherRegion", newRegion);
Defensive patterns

Strategy: try-catch

Validate before calling

if (!string.IsNullOrEmpty(region.Name) && region.Name != newName)
    throw new InvalidOperationException($"Region already named {region.Name}");
region.Name = newName;

Try / catch

try
{
    region.Name = newName;
}
catch (InvalidOperationException)
{
    // create a new Region with the desired name instead of renaming
}

Prevention

When it happens

Trigger: Setting region.Name = "X" when the region's current _name is non-null and different — e.g. reusing a Region object for another region name, or a custom region adapter assigning a new name to an existing region instance.

Common situations: Custom IRegionAdapter implementations that reuse region instances; re-registering a region with a new name at runtime; unit tests reusing a Region across cases.

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


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

Appendix: source

Thrown at src/Maui/Prism.Maui/Navigation/Regions/Region.cs:106

    public object Context
    {
        get => _context;
        set => SetProperty(ref _context, value);
    }

    private string _name;
    /// <summary>
    /// Gets the name of the region that uniquely identifies the region within a <see cref="IRegionManager"/>.
    /// </summary>
    /// <value>The name of the region.</value>
    public string Name
    {
        get => _name;
        set
        {
            if (_name != null && _name != value)
            {
                throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Resources.CannotChangeRegionNameException, _name));
            }

            if (string.IsNullOrEmpty(value))
            {
                throw new ArgumentException(Resources.RegionNameCannotBeEmptyException);
            }

            _name = value;
            RaisePropertyChanged(nameof(Name));
        }
    }

    /// <summary>
    /// Gets or sets the comparison used to sort the views.
    /// </summary>
    /// <value>The comparison to use.</value>
    public Comparison<object> SortComparison
    {

View on GitHub (pinned to 358118cd64)