PrismLibrary/Prism · error · ArgumentNullException

ArgumentNullException for 'view'

Error message

ArgumentNullException for 'view'

What it means

RegionManager.GetObservableRegion returns (or lazily creates) an ObservableObject<IRegion> wrapper stored on the view, allowing callers to observe when the region changes. It throws ArgumentNullException when the view is null because no wrapper can be attached to a null element.

Solutions

  1. Pass the view (VisualElement) that hosts the region, not a ViewModel or null.
  2. Ensure the view's visual tree element exists before obtaining the observable wrapper.

Example fix

// before
RegionManager.GetObservableRegion(null).Value = region;
// after
if (view is not null)
    RegionManager.GetObservableRegion(view).Value = region;
Defensive patterns

Strategy: type-guard

Validate before calling

if (view is null) throw new InvalidOperationException("View must exist before obtaining observable region");

Type guard

if (view is VisualElement ve) { var wrapper = RegionManager.GetObservableRegion(ve); }

Try / catch

try { RegionManager.GetObservableRegion(view); }
catch (ArgumentNullException) { logger.LogWarning("GetObservableRegion called with null view"); }

Prevention

When it happens

Trigger: Calling RegionManager.GetObservableRegion(null).

Common situations: Subscribing to region changes in a page whose content element was not yet created; passing a ViewModel instead of the view.

Related errors


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

Appendix: source

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

        if (regionTarget == null)
            throw new ArgumentNullException(nameof(regionTarget));

        return regionTarget.GetValue(RegionNameProperty) as string;
    }

    /// <summary>
    /// Returns an <see cref="ObservableObject{T}"/> wrapper that can hold an <see cref="IRegion"/>. Using this wrapper
    /// you can detect when an <see cref="IRegion"/> has been created by the <see cref="RegionAdapterBase{T}"/>.
    ///
    /// If the <see cref="ObservableObject{T}"/> wrapper does not yet exist, a new wrapper will be created. When the region
    /// gets created and assigned to the wrapper, you can use the <see cref="ObservableObject{T}"/> event
    /// to get notified of that change.
    /// </summary>
    /// <param name="view">The view that will host the region. </param>
    /// <returns>Wrapper that can hold an <see cref="IRegion"/> value and can notify when the <see cref="IRegion"/> value changes. </returns>
    public static ObservableObject<IRegion> GetObservableRegion(VisualElement view)
    {
        if (view is null) throw new ArgumentNullException(nameof(view));

        if (view.GetValue(ObservableRegionProperty) is not ObservableObject<IRegion> regionWrapper)
        {
            regionWrapper = new ObservableObject<IRegion>();
            view.SetValue(ObservableRegionProperty, regionWrapper);
        }

        return regionWrapper;
    }

    private static void OnSetRegionNameCallback(BindableObject bindable, object oldValue, object newValue)
    {
        if (DesignMode.IsDesignModeEnabled || newValue is null || bindable is not VisualElement view)
            return;

        CreateRegion(view);
    }

View on GitHub (pinned to 358118cd64)