{"record":{"id":"b9ca0e6f73488337","repo":"dotnet/maui","slug":"element-is-not-of-type-typeof-telement","errorCode":null,"errorMessage":"element is not of type \" + typeof(TElement)","messagePattern":"element is not of type \" \\+ typeof\\(TElement\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Compatibility/Core/src/Android/VisualElementRenderer.cs","lineNumber":116,"sourceCode":"\n\t\tVisualElement IVisualElementRenderer.Element => Element;\n\n\t\tevent EventHandler<VisualElementChangedEventArgs> IVisualElementRenderer.ElementChanged\n\t\t{\n\t\t\tadd { _elementChangedHandlers.Add(value); }\n\t\t\tremove { _elementChangedHandlers.Remove(value); }\n\t\t}\n\n\t\tpublic virtual SizeRequest GetDesiredSize(int widthConstraint, int heightConstraint)\n\t\t{\n\t\t\tMeasure(widthConstraint, heightConstraint);\n\t\t\treturn new SizeRequest(new Size(MeasuredWidth, MeasuredHeight), MinimumSize());\n\t\t}\n\n\t\tvoid IVisualElementRenderer.SetElement(VisualElement element)\n\t\t{\n\t\t\tif (!(element is TElement))\n\t\t\t\tthrow new ArgumentException(\"element is not of type \" + typeof(TElement), nameof(element));\n\n\t\t\tSetElement((TElement)element);\n\t\t}\n\n\t\tpublic VisualElementTracker Tracker { get; private set; }\n\n\t\tpublic void UpdateLayout()\n\t\t{\n\t\t\tPerformance.Start(out string reference);\n\t\t\tTracker?.UpdateLayout();\n\t\t\tPerformance.Stop(reference);\n\t\t}\n\n\t\tAView IVisualElementRenderer.View => this;\n\n\t\tpublic event EventHandler<ElementChangedEventArgs<TElement>> ElementChanged;\n\t\tpublic event EventHandler<PropertyChangedEventArgs> ElementPropertyChanged;\n","sourceCodeStart":98,"sourceCodeEnd":134,"githubUrl":"https://github.com/dotnet/maui/blob/f377ff1c5ee04d334d8a925f50c83a6b7afddf03/src/Compatibility/Core/src/Android/VisualElementRenderer.cs#L98-L134","documentation":"VisualElementRenderer<TElement> is the generic base for the (legacy/shell) Android renderer pipeline. Its explicit SetElement checks `element is TElement` and throws ArgumentException if false, then delegates to the protected SetElement(TElement). TElement is the concrete control type the renderer was built for.","triggerScenarios":"SetElement called with an element whose runtime type is not assignable to the renderer's TElement generic parameter. Typical of an incorrect ExportRenderer registration or a manual call.","commonSituations":"ExportRenderer pairs a renderer generic with a mismatched control; a custom renderer subclass changes TElement but the attribute still names the old control; handler/router dispatches the wrong renderer type.","solutions":["Match ExportRenderer(typeof(TElement), typeof(VisualElementRenderer<TElement> subclass)) so the element type aligns with TElement.","When subclassing, set TElement to the exact control type the renderer handles.","Avoid invoking SetElement directly with elements of a different type."],"exampleFix":"// before\n[assembly: ExportRenderer(typeof(ScrollView), typeof(MyEntryRenderer))]\n// after — TElement aligns with the registered control\n[assembly: ExportRenderer(typeof(Entry), typeof(MyEntryRenderer))] // where MyEntryRenderer : VisualElementRenderer<Entry>","handlingStrategy":"type-guard","validationCode":"static void Attach<TElement>(VisualElementRenderer<TElement> r, VisualElement e) where TElement : VisualElement\n{\n    if (e is null) throw new ArgumentNullException(nameof(e));\n    if (e is not TElement typed)\n        throw new ArgumentException($\"Expected {typeof(TElement).Name}, got {e.GetType().Name}.\", nameof(e));\n    ((IVisualElementRenderer)r).SetElement(typed);\n}","typeGuard":"static bool MatchesRenderer<TElement>(VisualElement e) where TElement : VisualElement => e is TElement;","tryCatchPattern":null,"preventionTips":["Keep ExportRenderer control type equal to the renderer's TElement.","When subclassing, set TElement to the exact control type.","Avoid manual SetElement with mismatched types."],"tags":["android","renderer","type-mismatch","generic","base-renderer","maui-compat"],"backgroundTag":null,"analyzedSha":"f377ff1c5ee04d334d8a925f50c83a6b7afddf03","analyzedAt":"2026-08-13T14:26:18.069Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}