PrismLibrary/Prism · error · ArgumentNullException

ArgumentNullException for 'target'

Error message

ArgumentNullException for 'target'

What it means

RegionManager.GetRegionManager retrieves the IRegionManager attached to a VisualElement via the RegionManager attached property. It throws ArgumentNullException when the target element is null, as no attached value can be read.

Solutions

  1. Pass the actual VisualElement (e.g. the page or its content region host).
  2. Null-check the element before retrieving the region manager.
  3. If a null region manager result is also possible, cast-check or null-check the returned value.

Example fix

// before
var rm = RegionManager.GetRegionManager(null);
// after
var rm = element is not null ? RegionManager.GetRegionManager(element) : null;
Defensive patterns

Strategy: type-guard

Validate before calling

if (target is null) return null;

Type guard

var rm = target is VisualElement el ? RegionManager.GetRegionManager(el) : null;

Try / catch

try { rm = RegionManager.GetRegionManager(element); }
catch (ArgumentNullException) { rm = null; }

Prevention

When it happens

Trigger: Calling RegionManager.GetRegionManager(null).

Common situations: Resolving a scoped region manager from a page whose content is null; passing a ViewModel or service instead of a VisualElement.

Related errors


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

Appendix: source

Thrown at src/Maui/Prism.Maui/Navigation/Regions/Xaml/RegionManager.cs:155

        CreateRegion(view);
    }

    private static void CreateRegion(VisualElement element)
    {
        var regionCreationBehavior = ContainerLocator.Container.Resolve<DelayedRegionCreationBehavior>();
        regionCreationBehavior.TargetElement = element;
        regionCreationBehavior.Attach();
    }

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

        return (IRegionManager)target.GetValue(RegionManagerProperty);
    }

    /// <summary>
    /// Sets the <see cref="RegionManagerProperty"/> attached property.
    /// </summary>
    /// <param name="target">The target element.</param>
    /// <param name="value">The value.</param>
    public static void SetRegionManager(VisualElement target, IRegionManager value)
    {
        if (target == null)
            throw new ArgumentNullException(nameof(target));

        target.SetValue(RegionManagerProperty, value);
    }

    private static void OnRegionContextChanged(BindableObject bindable, object oldValue, object newValue)

View on GitHub (pinned to 358118cd64)