{"record":{"id":"de745347fdb5b509","repo":"dotnet/maui","slug":"element-is-not-of-type-typeof-telement-de7453","errorCode":null,"errorMessage":"Element is not of type {typeof(TElement)}","messagePattern":"Element is not of type (.+?)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Compatibility/Core/src/Tizen/Renderers/VisualElementRenderer.cs","lineNumber":211,"sourceCode":"\n\t\t\tnewElement.IsPlatformEnabled = true;\n\n\t\t\tOnElementReady();\n\n\t\t\tSendVisualElementInitialized(newElement, NativeView);\n\t\t}\n\n\t\tvoid IVisualElementRenderer.UpdateLayout()\n\t\t{\n\t\t\tUpdateLayout();\n\t\t}\n\n\t\tvoid IVisualElementRenderer.SetElement(VisualElement element)\n\t\t{\n\t\t\tTElement tElement = element as TElement;\n\t\t\tif (tElement == null)\n\t\t\t{\n\t\t\t\tthrow new ArgumentException(\"Element is not of type \" + typeof(TElement), nameof(element));\n\t\t\t}\n\t\t\tSetElement(tElement);\n\t\t}\n\n\t\t/// <summary>\n\t\t/// Registers the effect with the element by establishing the parent-child relations needed for rendering on the specific platform.\n\t\t/// </summary>\n\t\t/// <param name=\"effect\">The effect to register.</param>\n\t\tvoid IEffectControlProvider.RegisterEffect(Effect effect)\n\t\t{\n\t\t\tRegisterEffect(effect);\n\t\t}\n\n\t\t/// <summary>\n\t\t/// Registers the effect with the element by establishing the parent-child relations needed for rendering on the specific platform.\n\t\t/// </summary>\n\t\t/// <param name=\"effect\">The effect to register.</param>\n\t\tprotected void RegisterEffect(Effect effect)","sourceCodeStart":193,"sourceCodeEnd":229,"githubUrl":"https://github.com/dotnet/maui/blob/f377ff1c5ee04d334d8a925f50c83a6b7afddf03/src/Compatibility/Core/src/Tizen/Renderers/VisualElementRenderer.cs#L193-L229","documentation":"The explicit-interface implementation IVisualElementRenderer.SetElement(VisualElement) on Tizen's VisualElementRenderer<TElement> throws ArgumentException when the incoming VisualElement cannot be cast to the renderer's generic TElement type. The renderer is registered against one element type but the framework routed a different subtype to it.","triggerScenarios":"The Tizen renderer registrar resolves this renderer for a VisualElement that is not assignable to TElement; a custom renderer is registered with the wrong ExportRenderer attribute or wrong generic parameter; DataTemplate returns a mismatched element type.","commonSituations":"Registering CustomButtonRenderer : VisualElementRenderer<MyCustomButton> against a Button export; switching an element's class without updating the renderer registration; shared renderer reused for sibling element types.","solutions":["Verify the ExportRenderer attribute's first type argument matches the generic TElement of the renderer.","Make TElement the common base type if the renderer must serve multiple element subtypes.","In custom renderers, prefer overriding SetElement(TElement) over re-implementing the interface to avoid bypassing the cast.","Check that DataTemplates / page factories produce elements assignable to the registered type."],"exampleFix":"// before\n[assembly: ExportRenderer(typeof(Button), typeof(CustomButtonRenderer<MyCustomButton>))]\n\n// after\n[assembly: ExportRenderer(typeof(MyCustomButton), typeof(CustomButtonRenderer<MyCustomButton>))]","handlingStrategy":"type-guard","validationCode":"if (element is TElement typed) renderer.SetElement(typed);\nelse throw new InvalidOperationException($\"Expected {typeof(TElement)}, got {element?.GetType()}\");","typeGuard":"static bool IsExpectedElement(VisualElement e) => e is TElement;","tryCatchPattern":"try { ((IVisualElementRenderer)renderer).SetElement(element); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"not of type\"))\n{ /* registrar mismatch; log element type and renderer generic */ }","preventionTips":["Keep ExportRenderer attribute type argument aligned with the renderer's generic parameter.","Add a unit test asserting the registrar returns the expected renderer type for each element.","Avoid sharing one renderer class across unrelated element types."],"tags":["tizen","renderer","type-mismatch","argument","registrar"],"backgroundTag":null,"analyzedSha":"f377ff1c5ee04d334d8a925f50c83a6b7afddf03","analyzedAt":"2026-08-13T14:26:18.069Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}