PrismLibrary/Prism · error · InvalidOperationException
View already exists in region.
Error message
View already exists in region.
What it means
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.
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.
Example fix
// before
region.Add(contentView);
// after
if (region.Views.Cast<object>().All(v => v != contentView))
{
region.Add(contentView);
} Defensive patterns
Strategy: validation
Validate before calling
bool exists = region.Views.Cast<object>().Contains(view);
Type guard
static bool IsInRegion(IRegion region, object view) => region.Views.Cast<object>().Any(v => ReferenceEquals(v, view));
Try / catch
try { region.Add(view, name); } catch (InvalidOperationException ex) { logger.LogWarning(ex, "View already added to region {Region}", region.Name); } Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Understand the failure class
Background: "already exists" / EEXIST / FileAlreadyExistsException: what the 'file already exists' error means and how to fix it — this error's family across 37 libraries.
Related errors
- Layout 's Children property is not empty. This control is…
- regionName
- regionTarget
- The object must be of type
- Mapping with the given type is already registered
AI-assisted analysis of PrismLibrary/Prism@358118cd64 (2026-09-15).
Data as JSON: /api/errors/95f2c3e3bfe8fe7f.
Report an issue: GitHub.
Appendix: source
Thrown at src/Maui/Prism.Maui/Navigation/Regions/Region.cs:272
/// <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>
/// <returns>The <see cref="IRegionManager"/> that is set on the view if it is a <see cref="VisualElement"/>.</returns>
public virtual IRegionManager Add(object view, string viewName, bool createRegionManagerScope)
{
IRegionManager manager = createRegionManagerScope ? RegionManager.CreateRegionManager() : RegionManager;
InnerAdd(view, viewName, manager);
return manager;
}
private void InnerAdd(object view, string viewName, IRegionManager scopedRegionManager)
{
if (view is not VisualElement visualElement)
{
throw new UpdateRegionsException("The view must inherit from VisualElement.");
}
if (ItemMetadataCollection.FirstOrDefault(x => x.Item == view) != null)
{
throw new InvalidOperationException(Resources.RegionViewExistsException);
}
var itemMetadata = new ItemMetadata(visualElement);
if (!string.IsNullOrEmpty(viewName))
{
if (ItemMetadataCollection.FirstOrDefault(x => x.Name == viewName) != null)
{
throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, Resources.RegionViewNameExistsException, viewName));
}
itemMetadata.Name = viewName;
}
Xaml.RegionManager.SetRegionManager(visualElement, scopedRegionManager);
ItemMetadataCollection.Add(itemMetadata);
}
/// <summary>View on GitHub (pinned to 358118cd64)