{"record":{"id":"95f2c3e3bfe8fe7f","repo":"PrismLibrary/Prism","slug":"view-already-exists-in-region","errorCode":null,"errorMessage":"View already exists in region.","messagePattern":"View already exists in region\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Maui/Prism.Maui/Navigation/Regions/Region.cs","lineNumber":272,"sourceCode":"    /// <param name=\"createRegionManagerScope\">When <see langword=\"true\"/>, the added view will receive a new instance of <see cref=\"IRegionManager\"/>, otherwise it will use the current region manager for this region.</param>\n    /// <returns>The <see cref=\"IRegionManager\"/> that is set on the view if it is a <see cref=\"VisualElement\"/>.</returns>\n    public virtual IRegionManager Add(object view, string viewName, bool createRegionManagerScope)\n    {\n        IRegionManager manager = createRegionManagerScope ? RegionManager.CreateRegionManager() : RegionManager;\n        InnerAdd(view, viewName, manager);\n        return manager;\n    }\n\n    private void InnerAdd(object view, string viewName, IRegionManager scopedRegionManager)\n    {\n        if (view is not VisualElement visualElement)\n        {\n            throw new UpdateRegionsException(\"The view must inherit from VisualElement.\");\n        }\n\n        if (ItemMetadataCollection.FirstOrDefault(x => x.Item == view) != null)\n        {\n            throw new InvalidOperationException(Resources.RegionViewExistsException);\n        }\n\n        var itemMetadata = new ItemMetadata(visualElement);\n        if (!string.IsNullOrEmpty(viewName))\n        {\n            if (ItemMetadataCollection.FirstOrDefault(x => x.Name == viewName) != null)\n            {\n                throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, Resources.RegionViewNameExistsException, viewName));\n            }\n            itemMetadata.Name = viewName;\n        }\n\n        Xaml.RegionManager.SetRegionManager(visualElement, scopedRegionManager);\n\n        ItemMetadataCollection.Add(itemMetadata);\n    }\n\n    /// <summary>","sourceCodeStart":254,"sourceCodeEnd":290,"githubUrl":"https://github.com/PrismLibrary/Prism/blob/358118cd640d9a22ff8cf21c8ad197fa038b7990/src/Maui/Prism.Maui/Navigation/Regions/Region.cs#L254-L290","documentation":"Region.InnerAdd throws InvalidOperationException (Resources.RegionViewExistsException) when the exact view object being added is already present in the region's ItemMetadataCollection. A region tracks each added view instance once; adding the same instance again is a state error, not a naming issue.","triggerScenarios":"Calling region.Add(view) or region.Add(view, \"name\") twice with the same object instance without removing it first; re-adding a view returned by region.GetView; region re-initialization code that re-runs Add on existing content.","commonSituations":"Page lifecycle callbacks (OnAppearing, NavigatedTo) that re-add views; duplicate initialization on region attach; manual region population combined with automatic region population; accidentally sharing one view instance across multiple Add calls.","solutions":["Check region.GetView(name) or iterate region.Views before calling Add and skip if the view already exists.","Call region.Remove(view) before re-adding the same instance.","Guard re-entrancy: only run the Add code once (e.g. a bool flag or check in OnAppearing).","Create a new view instance for each Add instead of reusing one object."],"exampleFix":"// before\nregion.Add(contentView);\n\n// after\nif (region.Views.Cast<object>().All(v => v != contentView))\n{\n    region.Add(contentView);\n}","handlingStrategy":"validation","validationCode":"bool exists = region.Views.Cast<object>().Contains(view);","typeGuard":"static bool IsInRegion(IRegion region, object view) => region.Views.Cast<object>().Any(v => ReferenceEquals(v, view));","tryCatchPattern":"try { region.Add(view, name); } catch (InvalidOperationException ex) { logger.LogWarning(ex, \"View already added to region {Region}\", region.Name); }","preventionTips":["Guard all Add calls with a membership check on region.Views","Do not re-add views in OnAppearing/NavigatedTo lifecycle hooks","Create new view instances instead of reusing one object across Adds"],"tags":["prism","maui","regions","duplicate"],"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"}