PrismLibrary/Prism · error · ArgumentNullException

element

Error message

element

What it means

DefaultRegionManagerAccessor.GetRegionName throws ArgumentNullException when the VisualElement parameter 'element' is null. This accessor reads the RegionManager.RegionName attached property from an element so the RegionManager can discover region names.

Solutions

  1. Ensure the element whose RegionName is queried is alive and attached
  2. Null-check the element before calling GetRegionName
  3. Fix the behavior setup so it does not outlive its target control
  4. Guard against recycled/virtualized template elements that can be null

Example fix

// before
var name = accessor.GetRegionName(element);
// after
var name = element is null ? null : accessor.GetRegionName(element);
Defensive patterns

Strategy: type-guard

Validate before calling

var regionName = element is null ? null : DefaultRegionManagerAccessor.Instance.GetRegionName(element);

Type guard

string? SafeGetRegionName(VisualElement? e) => e is null ? null : accessor.GetRegionName(e);

Try / catch

try { name = accessor.GetRegionName(element); }
catch (ArgumentNullException) { name = null; }

Prevention

When it happens

Trigger: Passing null to GetRegionName — typically from a region behavior whose target element reference was lost (expired weak reference, unloaded control) or from custom code querying a region name on a non-existent element.

Common situations: Region behaviors surviving after their host was removed from the visual tree; custom region code paths iterating visual-tree children that include nulls; tests calling the accessor without an element.

Related errors


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

Appendix: source

Thrown at src/Maui/Prism.Maui/Navigation/Regions/DefaultRegionManagerAccessor.cs:27

    /// </summary>
    /// <remarks>This event uses weak references to the event handler to prevent this static event of keeping the
    /// target element longer than expected.</remarks>
    public event EventHandler UpdatingRegions
    {
        add => Xaml.RegionManager.UpdatingRegions += value;
        remove => Xaml.RegionManager.UpdatingRegions -= value;
    }

    /// <summary>
    /// Gets the value for the RegionName attached property.
    /// </summary>
    /// <param name="element">The object to adapt. This is typically a container (i.e a control).</param>
    /// <returns>The name of the region that should be created when
    /// the RegionManager is also set in this element.</returns>
    public string GetRegionName(VisualElement element)
    {
        if (element == null)
            throw new ArgumentNullException(nameof(element));

        return element.GetValue(Xaml.RegionManager.RegionNameProperty) as string;
    }

    /// <summary>
    /// Gets the value of the RegionName attached property.
    /// </summary>
    /// <param name="element">The target element.</param>
    /// <returns>The <see cref="IRegionManager"/> attached to the <paramref name="element"/> element.</returns>
    public IRegionManager GetRegionManager(VisualElement element)
    {
        if (element == null)
            throw new ArgumentNullException(nameof(element));

        var regionManager = TryGetRegion(element);
        if (regionManager is null)
        {
            regionManager = ContainerLocator.Container.Resolve<IRegionManager>();

View on GitHub (pinned to 358118cd64)