PrismLibrary/Prism · error · ArgumentNullException

regionTarget

Error message

regionTarget

What it means

RegionAdapterBase<T>.GetCastedObject receives the regionTarget as object and throws ArgumentNullException when it is null, before attempting the cast to the adapter's control type T. It is the first guard invoked from Initialize.

Solutions

  1. Fix the caller so a valid control instance is passed to Initialize / GetMapping
  2. Check that the view containing the region actually instantiated (no XAML compile/load failure producing null)
  3. If writing custom region setup code, validate the control for null before calling the adapter

Example fix

// before
adapter.Initialize(someLayout, "MyRegion"); // someLayout is null

// after
if (someLayout is null)
    throw new InvalidOperationException("Region target control was not resolved");
adapter.Initialize(someLayout, "MyRegion");
Defensive patterns

Strategy: type-guard

Validate before calling

if (regionTarget is null) throw new InvalidOperationException("Region target control was not resolved before Initialize");

Type guard

bool CanAdapt(object regionTarget) => regionTarget is not null;

Try / catch

try { adapter.Initialize(target, name); }
catch (ArgumentNullException) { /* target control was null; check XAML instantiation */ }

Prevention

When it happens

Trigger: Calling any IRegionAdapter.Initialize(null, "RegionName"), or resolving an adapter through RegionAdapterMappings.GetMapping(null). Happens when region discovery hands a null control to the adapter pipeline.

Common situations: Programmatically registering regions with an uninitialized control reference; a XAML element failing to instantiate so the adapter target is null; custom adapter code passing an unresolved view.

Related errors


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

Appendix: source

Thrown at src/Maui/Prism.Maui/Navigation/Regions/Adapters/RegionAdapterBase.cs:129

    /// <summary>
    /// Template method to adapt the object to an <see cref="IRegion"/>.
    /// </summary>
    /// <param name="region">The new region being used.</param>
    /// <param name="regionTarget">The object to adapt.</param>
    protected abstract void Adapt(IRegion region, T regionTarget);

    /// <summary>
    /// Template method to create a new instance of <see cref="IRegion"/>
    /// that will be used to adapt the object.
    /// </summary>
    /// <returns>A new instance of <see cref="IRegion"/>.</returns>
    protected abstract IRegion CreateRegion(IContainerProvider container);

    private static T GetCastedObject(object regionTarget)
    {
        if (regionTarget == null)
            throw new ArgumentNullException(nameof(regionTarget));

        if (regionTarget is not T castedObject)
            throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, Resources.AdapterInvalidTypeException, typeof(T).Name));

        return castedObject;
    }

    private static void SetObservableRegionOnHostingControl(IRegion region, T regionTarget)
    {
        if (regionTarget is VisualElement targetElement)
        {
            // Set the region as a dependency property on the control hosting the region
            // Because we are using an observable region, the hosting control can detect that the
            // region has actually been created. This is an ideal moment to hook up custom behaviors
            Xaml.RegionManager.GetObservableRegion(targetElement).Value = region;
        }
    }
}

View on GitHub (pinned to 358118cd64)