PrismLibrary/Prism · error · InvalidOperationException

View with name ' ' already exists in the region.

Error message

View with name '{0}' already exists in the region.

What it means

Region.InnerAdd throws InvalidOperationException (Resources.RegionViewNameExistsException, formatted with the view name) when you add a view under a viewName that is already used by another view in the region. View names are unique per region because GetView(name) must resolve unambiguously.

Solutions

  1. Uniquify the view name before Add (e.g. append an id/index) or ensure each named view is added only once.
  2. Remove or rename the existing view: get it via region.GetView(existingName) and region.Remove it before adding a new one under that name.
  3. Guard with a check on region.GetView(name) == null before adding.
  4. Use auto-generated names (leave viewName empty) if you do not need GetView-by-name.

Example fix

// before
region.Add(new DetailsView(), "details"); // second call throws

// after
if (region.GetView("details") == null)
{
    region.Add(new DetailsView(), "details");
}
Defensive patterns

Strategy: validation

Validate before calling

bool nameTaken = !string.IsNullOrEmpty(viewName) && region.GetView(viewName) != null;

Try / catch

try { region.Add(view, viewName); } catch (InvalidOperationException ex) { logger.LogWarning(ex, "View name '{Name}' already in region", viewName); }

Prevention

When it happens

Trigger: region.Add(viewA, "details") followed later by region.Add(viewB, "details") in the same region; re-running initialization code that re-registers views with the same names.

Common situations: Naming collisions when dynamically populating regions from a list or config; duplicate region registration in XAML + code-behind; multiple view models each registering with a shared constant 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/00191e35f011be03. Report an issue: GitHub.

Appendix: source

Thrown at src/Maui/Prism.Maui/Navigation/Regions/Region.cs:280

    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>
    /// Returns the view instance that was added to the region using a specific name.
    /// </summary>
    /// <param name="viewName">The name used when adding the view to the region.</param>
    /// <returns>Returns the named view or <see langword="null"/> if the view with <paramref name="viewName"/> does not exist in the current region.</returns>
    public virtual object GetView(string viewName)
    {
        if (string.IsNullOrEmpty(viewName))
        {

View on GitHub (pinned to 358118cd64)