PrismLibrary/Prism · error · InvalidOperationException

The object must be of type

Error message

The object must be of type '{0}' in order to use the current region adapter.

What it means

RegionAdapterBase<T>.GetCastedObject throws InvalidOperationException (Resources.AdapterInvalidTypeException) when the regionTarget object is not an instance of the adapter's supported control type T. Each adapter only handles one control type; this guard rejects mismatched targets.

Solutions

  1. Ensure the control marked as a region matches the adapter type registered in ConfigureRegionAdapterMappings
  2. Register a custom adapter for the unsupported control type and map the correct TControl
  3. Fix custom adapters so the generic parameter T matches the control type they are mapped to

Example fix

// before
mappings.RegisterMapping<ScrollView, ContentViewRegionAdapter>(); // adapter targets ContentView

// after
mappings.RegisterMapping<ScrollView, ScrollViewRegionAdapter>();
Defensive patterns

Strategy: type-guard

Validate before calling

if (regionTarget is not ScrollView) throw new InvalidOperationException("Control type does not match the registered region adapter");

Type guard

bool MatchesAdapter<T>(object c) where T : class => c is T;

Try / catch

try { adapter.Initialize(target, name); }
catch (InvalidOperationException ex) when (ex.Message.Contains("region adapter")) { /* wrong control type for this adapter */ }

Prevention

When it happens

Trigger: Registering a mapping whose adapter's T does not match the control being adapted, or manually calling adapter.Initialize(controlOfWrongType, name) — e.g. passing a ContentView to ScrollViewRegionAdapter. Also occurs when RegisterMapping<TControl, TAdapter> declares TControl inconsistent with the adapter's supported type.

Common situations: Custom IRegionAdapter implementations written for one control type but registered for another; copy-pasted region registration code; framework upgrades where control hierarchies changed so GetType() no longer matches.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


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

Appendix: source

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

    /// </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)