PrismLibrary/Prism · error · ArgumentNullException

region

Error message

region

What it means

Fires in the RegionCollection indexer getter when the requested region name has not been registered with the RegionManager. UpdateRegions() runs first so XAML-declared regions are registered; if GetRegionByName still finds nothing, the caller asked for a region that does not exist (wrong name, or region never created/registered).

Solutions

  1. Null-check the region before adding; trace why the producer returned null.
  2. Fix the custom region factory/adapter so it returns a valid IRegion instance.
  3. Verify DI registration for the custom IRegion type actually maps to an implementation.

Example fix

// before
regionManager.Regions.Add(CreateRegion()); // may be null

// after
var region = CreateRegion();
if (region != null)
    regionManager.Regions.Add(region);
Defensive patterns

Strategy: validation

Validate before calling

if (region == null) throw new InvalidOperationException("Region factory returned null");

Try / catch

try { regionManager.Regions.Add(region); } catch (ArgumentNullException ex) { logger.LogError(ex, "Attempted to add null region"); }

Prevention

When it happens

Trigger: Calling regionManager.Regions.Add(null) directly, or a factory/GetRegion callback that returned null and its result is passed to Add.

Common situations: Custom IRegion creation code where a region resolver/adapter returned null; DI container returning null for a region type; test code constructing region collections.

Related errors


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

Appendix: source

Thrown at src/Maui/Prism.Maui/Navigation/Regions/RegionCollection.cs:50

    {
        get
        {
            Xaml.RegionManager.UpdateRegions();

            IRegion region = GetRegionByName(regionName);
            if (region == null)
            {
                throw new KeyNotFoundException(string.Format(CultureInfo.CurrentUICulture, Resources.RegionNotInRegionManagerException, regionName));
            }

            return region;
        }
    }

    public void Add(IRegion region)
    {
        if (region == null)
            throw new ArgumentNullException(nameof(region));

        Xaml.RegionManager.UpdateRegions();

        if (region.Name == null)
        {
            throw new InvalidOperationException(Resources.RegionNameCannotBeEmptyException);
        }

        if (GetRegionByName(region.Name) != null)
        {
            throw new ArgumentException(string.Format(CultureInfo.InvariantCulture,
                                                      Resources.RegionNameExistsException, region.Name));
        }

        _regions.Add(region);
        region.RegionManager = regionManager;

        OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, region, 0));

View on GitHub (pinned to 358118cd64)