PrismLibrary/Prism · error · ArgumentException

Resources.ViewNotInRegionException

Error message

Resources.ViewNotInRegionException

What it means

Region.GetItemMetadataOrThrow throws ArgumentException with ViewNotInRegionException for parameter "view" when the supplied view instance is not found in the region's ItemMetadataCollection. Methods like Activate, Deactivate, and Remove delegate to this helper, so operating on a view the region doesn't contain fails here.

Solutions

  1. Add the view to the region before activating/deactivating/removing it.
  2. Confirm you are using the exact registered instance (e.g. retrieved from region.Views or region.GetView(name)), not a new instance.
  3. Guard with region.Views.Contains(view) before calling the operation.

Example fix

// before
region.Activate(myView); // may not belong to this region
// after
if (region.Views.Contains(myView))
    region.Activate(myView);
Defensive patterns

Strategy: validation

Validate before calling

if (view == null || !region.Views.Contains(view))
    return; // view not managed by this region
region.Activate(view);

Try / catch

try
{
    region.Remove(view);
}
catch (ArgumentException)
{
    // view was not in this region — nothing to remove
}

Prevention

When it happens

Trigger: Calling region.Remove(view), region.Activate(view), or region.Deactivate(view) with a view instance that was never added to this region (or was already removed), or with a different instance of the same view type.

Common situations: Cross-region confusion — activating a view in region A that lives in region B; using a new'd-up view instance instead of the registered one; stale references kept after Remove.

Understand the failure class

Background: 'Could not be found', 'does not exist', 'not found in database': the resource-not-found family when an ID, slug, key, or URI lookup comes back empty — this error's family across 20 libraries.

Related errors


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

Appendix: source

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

#else
            if (view is AvaloniaObject avaloniaObject)
            {
                Regions.RegionManager.SetRegionManager(avaloniaObject, scopedRegionManager);
            }
#endif

            ItemMetadataCollection.Add(itemMetadata);
        }

        private ItemMetadata GetItemMetadataOrThrow(object view)
        {
            if (view == null)
                throw new ArgumentNullException(nameof(view));

            ItemMetadata itemMetadata = ItemMetadataCollection.FirstOrDefault(x => x.Item == view);

            if (itemMetadata == null)
                throw new ArgumentException(Resources.ViewNotInRegionException, nameof(view));

            return itemMetadata;
        }

        private void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
        }

        /// <summary>
        /// The default sort algorithm.
        /// </summary>
        /// <param name="x">The first view to compare.</param>
        /// <param name="y">The second view to compare.</param>
        /// <returns></returns>
        [SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "y")]
        [SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "x")]
        public static int DefaultSortComparison(object x, object y)

View on GitHub (pinned to 358118cd64)