PrismLibrary/Prism · error · InvalidOperationException

Resources.RegionViewExistsException

Error message

Resources.RegionViewExistsException

What it means

Region.InnerAdd throws InvalidOperationException with RegionViewExistsException when the exact view object instance is already present in the region's ItemMetadataCollection. Prism forbids adding the same view instance twice to a region.

Solutions

  1. Check region.Views.Contains(view) before calling Add; if present, call region.Activate(view) instead.
  2. Cache whether the view was already added (e.g. a bool flag or view.IsLoaded check).
  3. If a fresh visual state is needed, create a new view instance rather than re-adding the old one.

Example fix

// before
region.Add(myView); // throws on second call
// after
if (!region.Views.Contains(myView))
    region.Add(myView);
else
    region.Activate(myView);
Defensive patterns

Strategy: type-guard

Validate before calling

if (region.Views.Contains(view))
{
    region.Activate(view);
    return;
}
region.Add(view);

Try / catch

try
{
    region.Add(view);
}
catch (InvalidOperationException)
{
    region.Activate(view); // already present
}

Prevention

When it happens

Trigger: Calling region.Add(sameViewInstance) twice, or region.Add(view, name) where the view instance is already registered under a different name.

Common situations: Re-activating a view by calling Add again instead of Activate; navigation re-triggered on the same view instance; event handlers firing twice (double-click, repeated initialization).

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


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

Appendix: source

Thrown at src/Wpf/Prism.Wpf/Navigation/Regions/Region.cs:396

            RequestNavigate(target, navigationCallback, null);
        }

        /// <summary>
        /// Initiates navigation to the specified target.
        /// </summary>
        /// <param name="target">The target.</param>
        /// <param name="navigationCallback">A callback to execute when the navigation request is completed.</param>
        /// <param name="navigationParameters">The navigation parameters specific to the navigation request.</param>
        public void RequestNavigate(Uri target, Action<NavigationResult> navigationCallback, INavigationParameters navigationParameters)
        {
            NavigationService.RequestNavigate(target, navigationCallback, navigationParameters);
        }

        private void InnerAdd(object view, string viewName, IRegionManager scopedRegionManager)
        {
            if (ItemMetadataCollection.FirstOrDefault(x => x.Item == view) != null)
            {
                throw new InvalidOperationException(Resources.RegionViewExistsException);
            }

            var itemMetadata = new ItemMetadata(view);
            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;
            }

#if !AVALONIA
            if (view is DependencyObject dependencyObject)
            {
                Regions.RegionManager.SetRegionManager(dependencyObject, scopedRegionManager);
            }

View on GitHub (pinned to 358118cd64)