PrismLibrary/Prism · error · InvalidOperationException

Resources.RegionViewNameExistsException

Error message

Resources.RegionViewNameExistsException

What it means

Region.InnerAdd throws InvalidOperationException formatted with RegionViewNameExistsException when a different view with the same viewName already exists in ItemMetadataCollection. View names are unique keys within a region.

Solutions

  1. Ensure view names are unique per region — e.g. include an id/counter: $"ItemView_{id}".
  2. Check whether the name is already taken via region.GetView(name) != null before adding, and pick a new name or remove the old view.
  3. If replacing is intended, call region.Remove(region.GetView(name)) first, then Add with the same name.

Example fix

// before
region.Add(newView, "Details"); // 'Details' may already exist
// after
if (region.GetView("Details") != null)
    region.Remove(region.GetView("Details"));
region.Add(newView, "Details");
Defensive patterns

Strategy: validation

Validate before calling

if (region.GetView(viewName) != null)
    region.Remove(region.GetView(viewName));
region.Add(newView, viewName);

Try / catch

try
{
    region.Add(newView, viewName);
}
catch (InvalidOperationException)
{
    // name already used: remove old view then retry, or use unique name
}

Prevention

When it happens

Trigger: Calling region.Add(viewB, "sameName") when another view instance was previously added with name "sameName", even though viewB itself is new.

Common situations: Generating view names from a counter/format string that doesn't advance; re-registering a named view after recreating the view instance; multiple VMs computing the same default name.

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/cd7fbd6294fec34d. Report an issue: GitHub.

Appendix: source

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

        /// <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);
            }
#else
            if (view is AvaloniaObject avaloniaObject)
            {
                Regions.RegionManager.SetRegionManager(avaloniaObject, scopedRegionManager);
            }
#endif

            ItemMetadataCollection.Add(itemMetadata);

View on GitHub (pinned to 358118cd64)