PrismLibrary/Prism · error · ArgumentNullException

target

Error message

target

What it means

ClearChildViewsRegionBehavior.GetClearChildViews is the getter for the ClearChildViews attached property and throws ArgumentNullException when the target BindableObject is null. This mirrors the MAUI attached-property convention that getters require a valid target.

Solutions

  1. Null-check the element before reading the ClearChildViews attached property
  2. Fix the upstream reference so a valid BindableObject is passed (the element may not exist yet or was already disposed)
  3. Use target.GetValue(ClearChildViewsProperty) guarded by a null check, or drop the read when the target is absent

Example fix

// before
var clear = ClearChildViewsRegionBehavior.GetClearChildViews(element); // element null

// after
var clear = element is not null && ClearChildViewsRegionBehavior.GetClearChildViews(element);
Defensive patterns

Strategy: type-guard

Validate before calling

var clear = element is null ? false : ClearChildViewsRegionBehavior.GetClearChildViews(element);

Type guard

bool GetClearChildViewsSafe(BindableObject target) => target is not null && ClearChildViewsRegionBehavior.GetClearChildViews(target);

Try / catch

try { var v = ClearChildViewsRegionBehavior.GetClearChildViews(target); }
catch (ArgumentNullException) { /* target not resolved; treat as default false */ }

Prevention

When it happens

Trigger: Calling ClearChildViewsRegionBehavior.GetClearChildViews(null), typically from code that reads the attached property off a control reference that failed to resolve or was not yet created.

Common situations: Reading the property in cleanup/navigation code where the page or element reference is null (element already unloaded/disposed); helper utilities querying the property on optional elements without null checks.

Related errors


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

Appendix: source

Thrown at src/Maui/Prism.Maui/Navigation/Regions/Behaviors/ClearChildViewsRegionBehavior.cs:32

    /// The behavior key.
    /// </summary>
    public const string BehaviorKey = "ClearChildViews";

    /// <summary>
    /// This attached property can be defined on a view to indicate that regions defined in it must be removed from the region manager when the parent view gets removed from a region.
    /// </summary>
    public static readonly BindableProperty ClearChildViewsProperty =
        BindableProperty.CreateAttached("ClearChildViews", typeof(bool), typeof(ClearChildViewsRegionBehavior), false);

    /// <summary>
    /// Gets the ClearChildViews attached property from a BindableObject.
    /// </summary>
    /// <param name="target">The object from which to get the value.</param>
    /// <returns>The value of the ClearChildViews attached property in the target specified.</returns>
    public static bool GetClearChildViews(BindableObject target)
    {
        if (target == null)
            throw new ArgumentNullException(nameof(target));

        return (bool)target.GetValue(ClearChildViewsProperty);
    }

    /// <summary>
    /// Sets the ClearChildViews attached property in a BindableObject.
    /// </summary>
    /// <param name="target">The object in which to set the value.</param>
    /// <param name="value">The value of to set in the target object's ClearChildViews attached property.</param>
    public static void SetClearChildViews(BindableObject target, bool value)
    {
        if (target == null)
            throw new ArgumentNullException(nameof(target));

        target.SetValue(ClearChildViewsProperty, value);
    }

    /// <summary>

View on GitHub (pinned to 358118cd64)