{"record":{"id":"63c5fa1457789d5a","repo":"PrismLibrary/Prism","slug":"resources-regionnameexistsexception-formatted-with-region","errorCode":null,"errorMessage":"Resources.RegionNameExistsException (formatted with region.Name)","messagePattern":"Resources\\.RegionNameExistsException \\(formatted with region\\.Name\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Maui/Prism.Maui/Navigation/Regions/RegionCollection.cs","lineNumber":61,"sourceCode":"            return region;\n        }\n    }\n\n    public void Add(IRegion region)\n    {\n        if (region == null)\n            throw new ArgumentNullException(nameof(region));\n\n        Xaml.RegionManager.UpdateRegions();\n\n        if (region.Name == null)\n        {\n            throw new InvalidOperationException(Resources.RegionNameCannotBeEmptyException);\n        }\n\n        if (GetRegionByName(region.Name) != null)\n        {\n            throw new ArgumentException(string.Format(CultureInfo.InvariantCulture,\n                                                      Resources.RegionNameExistsException, region.Name));\n        }\n\n        _regions.Add(region);\n        region.RegionManager = regionManager;\n\n        OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, region, 0));\n    }\n\n    public bool Remove(string regionName)\n    {\n        Xaml.RegionManager.UpdateRegions();\n\n        bool removed = false;\n\n        IRegion region = GetRegionByName(regionName);\n        if (region != null)\n        {","sourceCodeStart":43,"sourceCodeEnd":79,"githubUrl":"https://github.com/PrismLibrary/Prism/blob/358118cd640d9a22ff8cf21c8ad197fa038b7990/src/Maui/Prism.Maui/Navigation/Regions/RegionCollection.cs#L43-L79","documentation":"RegionCollection.Add(IRegion) throws ArgumentException when a region with the same Name is already registered in the RegionManager. Region names must be unique within a RegionCollection, so adding a duplicate-named region is rejected via Resources.RegionNameExistsException formatted with the offending region.Name.","triggerScenarios":"Calling regionManager.Regions.Add(region) (or Add(regionName, region)) when GetRegionByName(region.Name) already returns a region — i.e. a region with that exact Name string is already registered.","commonSituations":"Registering the same view/region twice during app startup (e.g. both XAML and code-behind registration), re-running initialization on page recreation in MAUI, or two regions accidentally assigned the same x:Name/string name.","solutions":["Check Regions.ContainsRegionWithName(region.Name) before adding and skip or reuse the existing region.","Remove the existing region first (Regions.Remove) if replacement is intended.","Ensure each region gets a unique name; audit duplicate registration code paths (XAML + code-behind, OnAppearing handlers)."],"exampleFix":"// before\nregionManager.Regions.Add(new Region { Name = \"MainRegion\" });\n// after\nif (!regionManager.Regions.ContainsRegionWithName(\"MainRegion\"))\n    regionManager.Regions.Add(new Region { Name = \"MainRegion\" });","handlingStrategy":"validation","validationCode":"if (region != null && !regionManager.Regions.ContainsRegionWithName(region.Name))\n    regionManager.Regions.Add(region);","typeGuard":"bool IsRegionRegistered(IRegionCollection regions, IRegion r) => r?.Name != null && regions.ContainsRegionWithName(r.Name);","tryCatchPattern":"try { regionManager.Regions.Add(region); }\ncatch (ArgumentException ex) { Console.WriteLine($\"Region '{region?.Name}' already exists: {ex.Message}\"); }","preventionTips":["Always guard with ContainsRegionWithName before Add","Centralize region registration in one startup path","Avoid registering regions in OnAppearing or other repeatedly-called lifecycle methods"],"tags":["prism","regions","duplicate-key","argumentexception"],"backgroundTag":"file-already-exists","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"}