{"record":{"id":"edb7cd02878f9c4d","repo":"dotnet/maui","slug":"nameof-element-must-be-of-type-typeof-itemsvie","errorCode":null,"errorMessage":"{nameof(element)} must be of type {typeof(ItemsView).Name}","messagePattern":"(.+?) must be of type (.+?)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Compatibility/Core/src/Android/CollectionView/ItemsViewRenderer.cs","lineNumber":100,"sourceCode":"\n\t\tpublic event EventHandler<PropertyChangedEventArgs> ElementPropertyChanged;\n\n\t\tSizeRequest IVisualElementRenderer.GetDesiredSize(int widthConstraint, int heightConstraint)\n\t\t{\n\t\t\tMeasure(widthConstraint, heightConstraint);\n\t\t\treturn new SizeRequest(new Size(MeasuredWidth, MeasuredHeight), new Size());\n\t\t}\n\n\t\tvoid IVisualElementRenderer.SetElement(VisualElement element)\n\t\t{\n\t\t\tif (element == null)\n\t\t\t{\n\t\t\t\tthrow new ArgumentNullException(nameof(element));\n\t\t\t}\n\n\t\t\tif (!(element is ItemsView))\n\t\t\t{\n\t\t\t\tthrow new ArgumentException($\"{nameof(element)} must be of type {typeof(ItemsView).Name}\");\n\t\t\t}\n\n\t\t\tvar oldElement = ItemsView;\n\t\t\tvar newElement = (TItemsView)element;\n\n\t\t\tTearDownOldElement(oldElement);\n\t\t\tSetUpNewElement(newElement);\n\n\t\t\tOnElementChanged(oldElement, newElement);\n\n\t\t\t// TODO hartez 2018/06/06 20:57:12 Find out what this does, and whether we really need it\t\n\t\t\telement.SendViewInitialized(this);\n\t\t}\n\n\t\tvoid IVisualElementRenderer.SetLabelFor(int? id)\n\t\t{\n\t\t\t// TODO hartez 2018/06/06 20:58:54 Rethink whether we need to have _defaultLabelFor as a class member\t\n\t\t\tif (_defaultLabelFor == null)","sourceCodeStart":82,"sourceCodeEnd":118,"githubUrl":"https://github.com/dotnet/maui/blob/f377ff1c5ee04d334d8a925f50c83a6b7afddf03/src/Compatibility/Core/src/Android/CollectionView/ItemsViewRenderer.cs#L82-L118","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Confirm the ExportRenderer attribute pairs the renderer with an ItemsView-derived type (CollectionView, CarouselView, etc.).","Check that a custom renderer subclass preserves the TItemsView generic constraint.","Avoid instantiating/calling SetElement on the renderer manually with an unrelated VisualElement."],"exampleFix":"// before\n[assembly: ExportRenderer(typeof(ListView), typeof(ItemsViewRenderer<CollectionView, ListViewAdapter>))]\n// after\n[assembly: ExportRenderer(typeof(CollectionView), typeof(CollectionViewRenderer))]","handlingStrategy":"validation","validationCode":"void AttachItemsView<TView>(ItemsViewRenderer<TView, IItemsViewSource<TView>> r, VisualElement e) where TView : ItemsView\n{\n    if (e is null) throw new ArgumentNullException(nameof(e));\n    if (e is not TView view)\n        throw new ArgumentException($\"Expected {typeof(TView).Name}, got {e.GetType().Name}.\", nameof(e));\n    ((IVisualElementRenderer)r).SetElement(view);\n}","typeGuard":"static bool IsItemsView(VisualElement e) => e is ItemsView;","tryCatchPattern":null,"preventionTips":["Verify ExportRenderer pairs ItemsView-derived types with ItemsViewRenderer subclasses.","Prefer CollectionViewRenderer/CarouselViewRenderer over the open generic.","Validate element type before manual SetElement calls."],"tags":["android","renderer","type-mismatch","collectionview","carouselview","maui-compat"],"backgroundTag":null,"analyzedSha":"f377ff1c5ee04d334d8a925f50c83a6b7afddf03","analyzedAt":"2026-08-13T14:26:18.069Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}