{"record":{"id":"d14e39b2ef3ef56d","repo":"PrismLibrary/Prism","slug":"cannot-change-the-region-name-once-is-set-the-current-region","errorCode":null,"errorMessage":"Cannot change the region name once is set. The current region name is '{0}'.","messagePattern":"Cannot change the region name once is set\\. The current region name is '(.+?)'\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Maui/Prism.Maui/Navigation/Regions/Region.cs","lineNumber":106,"sourceCode":"    public object Context\n    {\n        get => _context;\n        set => SetProperty(ref _context, value);\n    }\n\n    private string _name;\n    /// <summary>\n    /// Gets the name of the region that uniquely identifies the region within a <see cref=\"IRegionManager\"/>.\n    /// </summary>\n    /// <value>The name of the region.</value>\n    public string Name\n    {\n        get => _name;\n        set\n        {\n            if (_name != null && _name != value)\n            {\n                throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Resources.CannotChangeRegionNameException, _name));\n            }\n\n            if (string.IsNullOrEmpty(value))\n            {\n                throw new ArgumentException(Resources.RegionNameCannotBeEmptyException);\n            }\n\n            _name = value;\n            RaisePropertyChanged(nameof(Name));\n        }\n    }\n\n    /// <summary>\n    /// Gets or sets the comparison used to sort the views.\n    /// </summary>\n    /// <value>The comparison to use.</value>\n    public Comparison<object> SortComparison\n    {","sourceCodeStart":88,"sourceCodeEnd":124,"githubUrl":"https://github.com/PrismLibrary/Prism/blob/358118cd640d9a22ff8cf21c8ad197fa038b7990/src/Maui/Prism.Maui/Navigation/Regions/Region.cs#L88-L124","documentation":"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.","triggerScenarios":"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.","commonSituations":"Custom IRegionAdapter implementations that reuse region instances; re-registering a region with a new name at runtime; unit tests reusing a Region across cases.","solutions":["Create a new Region instance for the new name instead of renaming the existing one.","Keep region names constant per region lifetime; register all region names up front.","Remove the old region from the RegionManager and add a new one with the desired name."],"exampleFix":"// before\nregion.Name = \"OtherRegion\"; // throws if already named\n// after\nvar newRegion = new Region { Name = \"OtherRegion\" };\nregionManager.Regions.Add(\"OtherRegion\", newRegion);","handlingStrategy":"try-catch","validationCode":"if (!string.IsNullOrEmpty(region.Name) && region.Name != newName)\n    throw new InvalidOperationException($\"Region already named {region.Name}\");\nregion.Name = newName;","typeGuard":null,"tryCatchPattern":"try\n{\n    region.Name = newName;\n}\ncatch (InvalidOperationException)\n{\n    // create a new Region with the desired name instead of renaming\n}","preventionTips":["Treat Region.Name as write-once; assign only at construction.","Never reuse Region instances across different region names.","Generate region names deterministically in custom adapters."],"tags":["invalid-state","region","immutable-property","prism"],"backgroundTag":"invalid-state-transition","analyzedSha":"358118cd640d9a22ff8cf21c8ad197fa038b7990","analyzedAt":"2026-09-15T15:00:31.079Z","contentChangedAt":"2026-09-15T15:00:31.079Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}