{"record":{"id":"c40f912bf8f4dd3a","repo":"dotnet/maui","slug":"element-must-be-of-type-label","errorCode":null,"errorMessage":"Element must be of type Label","messagePattern":"Element must be of type Label","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Compatibility/Core/src/Android/FastRenderers/LabelRenderer.cs","lineNumber":155,"sourceCode":"\t\t\t_lastConstraintHeight = heightConstraint;\n\t\t\t_lastSizeRequest = result;\n\n\t\t\treturn result;\n\t\t}\n\n\t\t[PortHandler]\n\t\tprotected override void OnLayout(bool changed, int left, int top, int right, int bottom)\n\t\t{\n\t\t\tbase.OnLayout(changed, left, top, right, bottom);\n\t\t\tthis.RecalculateSpanPositions(Element, _spannableString, new SizeRequest(new Size(right - left, bottom - top)));\n\t\t\t_hasLayoutOccurred = true;\n\t\t}\n\n\t\tvoid IVisualElementRenderer.SetElement(VisualElement element)\n\t\t{\n\t\t\tvar label = element as Label;\n\t\t\tif (label == null)\n\t\t\t\tthrow new ArgumentException(\"Element must be of type Label\");\n\n\t\t\tElement = label;\n\t\t\t_motionEventHelper.UpdateElement(element);\n\t\t}\n\n\t\tvoid IVisualElementRenderer.SetLabelFor(int? id)\n\t\t{\n\t\t\tif (_defaultLabelFor == null)\n\t\t\t\t_defaultLabelFor = ViewCompat.GetLabelFor(this);\n\n\t\t\tViewCompat.SetLabelFor(this, (int)(id ?? _defaultLabelFor));\n\t\t}\n\n\t\tvoid IVisualElementRenderer.UpdateLayout()\n\t\t{\n\t\t\tVisualElementTracker tracker = _visualElementTracker;\n\t\t\ttracker?.UpdateLayout();\n\t\t}","sourceCodeStart":137,"sourceCodeEnd":173,"githubUrl":"https://github.com/dotnet/maui/blob/f377ff1c5ee04d334d8a925f50c83a6b7afddf03/src/Compatibility/Core/src/Android/FastRenderers/LabelRenderer.cs#L137-L173","documentation":"LabelRenderer.SetElement performs `element as Label` and throws ArgumentException('Element must be of type Label') when null. Like FrameRenderer, there is no dedicated null guard, so a null element yields the same message via the null `as` result.","triggerScenarios":"SetElement called with a non-Label element, or null. The check is `label == null`, which catches both non-Label and null inputs.","commonSituations":"Wrong ExportRenderer registration; a custom LabelRenderer subclass mapped to a non-Label control; passing null during a teardown/swap sequence.","solutions":["Register LabelRenderer (or subclass) only for Label or Label-derived types.","Avoid null element arguments — the renderer does not distinguish null from wrong type.","If subclassing for a control that does not extend Label, choose a different base renderer."],"exampleFix":"// before\nvar label = element as Label;\nif (label == null)\n    throw new ArgumentException(\"Element must be of type Label\");\n// after — explicit null guard\nif (element == null)\n    throw new ArgumentNullException(nameof(element));\nif (element is not Label label)\n    throw new ArgumentException(\"Element must be of type Label\", nameof(element));","handlingStrategy":"validation","validationCode":"if (element is null) throw new ArgumentNullException(nameof(element));\nif (element is not Label) throw new ArgumentException(\"Expected Label.\", nameof(element));\n((IVisualElementRenderer)renderer).SetElement(element);","typeGuard":"static bool IsLabel(VisualElement e) => e is Label;","tryCatchPattern":null,"preventionTips":["LabelRenderer conflates null and wrong-type — always null-check first.","Register LabelRenderer only for Label or Label-derived types.","Avoid manual SetElement with elements of unknown type."],"tags":["android","renderer","type-mismatch","label","fast-renderer","maui-compat"],"backgroundTag":null,"analyzedSha":"f377ff1c5ee04d334d8a925f50c83a6b7afddf03","analyzedAt":"2026-08-13T14:26:18.069Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}