dotnet/maui · error · ArgumentException

{nameof(element)} must be of type {typeof(ItemsView).Name}

Error message

{nameof(element)} must be of type {typeof(ItemsView).Name}

What it means

ItemsViewRenderer<TItemsView,TAdapter> is the generic Android base renderer for CollectionView/CarouselView. Its explicit SetElement rejects anything that is not an ItemsView (the TItemsView constraint base). The generic guard exists because the renderer casts to TItemsView immediately after for adapter wiring.

Source

Thrown at src/Compatibility/Core/src/Android/CollectionView/ItemsViewRenderer.cs:100

		public event EventHandler<PropertyChangedEventArgs> ElementPropertyChanged;

		SizeRequest IVisualElementRenderer.GetDesiredSize(int widthConstraint, int heightConstraint)
		{
			Measure(widthConstraint, heightConstraint);
			return new SizeRequest(new Size(MeasuredWidth, MeasuredHeight), new Size());
		}

		void IVisualElementRenderer.SetElement(VisualElement element)
		{
			if (element == null)
			{
				throw new ArgumentNullException(nameof(element));
			}

			if (!(element is ItemsView))
			{
				throw new ArgumentException($"{nameof(element)} must be of type {typeof(ItemsView).Name}");
			}

			var oldElement = ItemsView;
			var newElement = (TItemsView)element;

			TearDownOldElement(oldElement);
			SetUpNewElement(newElement);

			OnElementChanged(oldElement, newElement);

			// TODO hartez 2018/06/06 20:57:12 Find out what this does, and whether we really need it	
			element.SendViewInitialized(this);
		}

		void IVisualElementRenderer.SetLabelFor(int? id)
		{
			// TODO hartez 2018/06/06 20:58:54 Rethink whether we need to have _defaultLabelFor as a class member	
			if (_defaultLabelFor == null)

View on GitHub (pinned to f377ff1c5e)

Solutions

  1. Confirm the ExportRenderer attribute pairs the renderer with an ItemsView-derived type (CollectionView, CarouselView, etc.).
  2. Check that a custom renderer subclass preserves the TItemsView generic constraint.
  3. Avoid instantiating/calling SetElement on the renderer manually with an unrelated VisualElement.

Example fix

// before
[assembly: ExportRenderer(typeof(ListView), typeof(ItemsViewRenderer<CollectionView, ListViewAdapter>))]
// after
[assembly: ExportRenderer(typeof(CollectionView), typeof(CollectionViewRenderer))]
Defensive patterns

Strategy: validation

Validate before calling

void AttachItemsView<TView>(ItemsViewRenderer<TView, IItemsViewSource<TView>> r, VisualElement e) where TView : ItemsView
{
    if (e is null) throw new ArgumentNullException(nameof(e));
    if (e is not TView view)
        throw new ArgumentException($"Expected {typeof(TView).Name}, got {e.GetType().Name}.", nameof(e));
    ((IVisualElementRenderer)r).SetElement(view);
}

Type guard

static bool IsItemsView(VisualElement e) => e is ItemsView;

Prevention

When it happens

Trigger: SetElement invoked with an element that is not an ItemsView or its derived type (CollectionView, CarouselView, GroupableItemsView, etc.). Typical of an incorrect ExportRenderer mapping or a custom renderer subclass bound to an incompatible control.

Common situations: Registering ItemsViewRenderer (or a CarouselViewRenderer) against a control that is not an ItemsView derivative; migrating from ListView to CollectionView but keeping the old renderer attribute; handler misrouting in MAUI compatibility shim.

Related errors


AI-assisted analysis of dotnet/maui@f377ff1c5e (2026-08-13). Data as JSON: /api/errors/edb7cd02878f9c4d. Report an issue: GitHub.