PrismLibrary/Prism · error · ArgumentNullException

targetElement

Error message

targetElement

What it means

DelayedRegionCreationBehavior.CreateRegion throws ArgumentNullException when the VisualElement that should host the region (targetElement) is null. This method instantiates the region for a lazily-created host; without a host element there is nothing to adapt.

Solutions

  1. Ensure the element with RegionName stays alive and in the visual tree long enough for region creation
  2. Avoid removing/unloading the host control before its Loaded event fires
  3. Check that the RegionName attached property is set on an actual VisualElement instance, not a template placeholder
  4. Wrap region-hosted content so the host is retained until navigation completes

Example fix

// before
<Grid prism:RegionManager.RegionName="{Binding MaybeNullRegionName}" />
// after
<Grid IsVisible="{Binding HasRegionHost}"
      prism:RegionManager.RegionName="MyRegion" />
Defensive patterns

Strategy: type-guard

Validate before calling

if (targetElement is null || targetElement.Parent is null) return; // skip creation until parented

Type guard

bool CanCreateRegion(VisualElement? e) => e is not null && e.TryGetParentPage(out _);

Try / catch

try { var region = CreateRegion(element, name); }
catch (ArgumentNullException ex) { logger.Warn(ex, "Region host vanished before creation"); }

Prevention

When it happens

Trigger: The delayed creation behavior fires (e.g. on Loaded) but the tracked target element reference is null — the control was garbage-collected, removed from the visual tree, or the WeakReference expired before TryCreateRegion invoked CreateRegion.

Common situations: Regions declared inside controls that are unloaded/destroyed quickly; collection virtualization recycling items away; navigation replacing a page before the region behavior's timer/Loaded callback runs.

Related errors


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

Appendix: source

Thrown at src/Maui/Prism.Maui/Navigation/Regions/Behaviors/DelayedRegionCreationBehavior.cs:123

            {
                string regionName = RegionManagerAccessor.GetRegionName(TargetElement);
                CreateRegion(TargetElement, regionName);
                Track();
                _regionCreated = true;
            }
        }
    }

    /// <summary>
    /// Method that will create the region, by calling the right <see cref="IRegionAdapter"/>.
    /// </summary>
    /// <param name="targetElement">The target element that will host the <see cref="IRegion"/>.</param>
    /// <param name="regionName">Name of the region.</param>
    /// <returns>The created <see cref="IRegion"/></returns>
    protected virtual IRegion CreateRegion(VisualElement targetElement, string regionName)
    {
        if (targetElement == null)
            throw new ArgumentNullException(nameof(targetElement));

        try
        {
            if (!targetElement.TryGetParentPage(out var page))
                throw new Exception("The Target Element has not yet been parented and we cannot get the parent page.");

            // Build the region
            var container = page.GetContainerProvider();
            targetElement.SetContainerProvider(container);
            var regionAdapter = _regionAdapterMappings.GetMapping(targetElement.GetType());
            var region = regionAdapter.Initialize(targetElement, regionName);

            return region;
        }
        catch (Exception ex)
        {
            throw new RegionCreationException(string.Format(CultureInfo.CurrentCulture, Resources.RegionCreationException, regionName, ex), ex);
        }

View on GitHub (pinned to 358118cd64)