PrismLibrary/Prism · error · ArgumentNullException

ArgumentNullException for 'regionTarget'

Error message

ArgumentNullException for 'regionTarget'

What it means

RegionManager.SetRegionName sets the RegionName attached property on a VisualElement so a region can be created for it. The library throws ArgumentNullException because there is no meaningful target element to attach a region name to when the argument is null.

Solutions

  1. Pass the actual VisualElement (container control) you want the region attached to.
  2. Guard against null element references (e.g. from FindByName or binding results) before calling.
  3. Prefer XAML RegionName attached-property usage, which guarantees a live element.

Example fix

// before
RegionManager.SetRegionName(null, "MainRegion");
// after
if (myContainer != null)
    RegionManager.SetRegionName(myContainer, "MainRegion");
Defensive patterns

Strategy: validation

Validate before calling

if (regionTarget is null) throw new InvalidOperationException("Region target element must be created before SetRegionName");

Type guard

if (regionTarget is VisualElement element && !string.IsNullOrWhiteSpace(regionName)) RegionManager.SetRegionName(element, regionName);

Try / catch

try { RegionManager.SetRegionName(target, name); }
catch (ArgumentNullException) { logger.LogWarning("Skipped SetRegionName: null target"); }

Prevention

When it happens

Trigger: Calling RegionManager.SetRegionName(null, "SomeRegion") — passing a null VisualElement as regionTarget.

Common situations: Programmatically wiring regions before a control is instantiated; a data-bound or factory-returned element that is null; refactoring where the element variable was removed.

Related errors


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

Appendix: source

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

    /// <summary>
    /// Gets the value of the <see cref="DefaultViewProperty"/> attached property for the specified region target.
    /// </summary>
    /// <param name="regionTarget">The <see cref="VisualElement"/> that hosts the default view.</param>
    /// <returns>
    /// The default view associated with the region. This can be a view name, a type, or an instance of the view.
    /// </returns>
    public static object GetDefaultView(VisualElement regionTarget) =>
        regionTarget.GetValue(DefaultViewProperty);

    /// <summary>
    /// Sets the <see cref="RegionNameProperty"/> attached property.
    /// </summary>
    /// <param name="regionTarget">The object to adapt. This is typically a container (i.e a control).</param>
    /// <param name="regionName">The name of the region to register.</param>
    public static void SetRegionName(VisualElement regionTarget, string regionName)
    {
        if (regionTarget == null)
            throw new ArgumentNullException(nameof(regionTarget));

        regionTarget.SetValue(RegionNameProperty, regionName);
    }

    /// <summary>
    /// Gets the value for the <see cref="RegionNameProperty"/> attached property.
    /// </summary>
    /// <param name="regionTarget">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
    /// <see cref="RegionManagerProperty"/> is also set in this element.</returns>
    public static string GetRegionName(VisualElement regionTarget)
    {
        if (regionTarget == null)
            throw new ArgumentNullException(nameof(regionTarget));

        return regionTarget.GetValue(RegionNameProperty) as string;
    }

View on GitHub (pinned to 358118cd64)