dotnet/maui · error · ArgumentException

Element must be of type Label

Error message

Element must be of type Label

What it means

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.

Source

Thrown at src/Compatibility/Core/src/Android/FastRenderers/LabelRenderer.cs:155

			_lastConstraintHeight = heightConstraint;
			_lastSizeRequest = result;

			return result;
		}

		[PortHandler]
		protected override void OnLayout(bool changed, int left, int top, int right, int bottom)
		{
			base.OnLayout(changed, left, top, right, bottom);
			this.RecalculateSpanPositions(Element, _spannableString, new SizeRequest(new Size(right - left, bottom - top)));
			_hasLayoutOccurred = true;
		}

		void IVisualElementRenderer.SetElement(VisualElement element)
		{
			var label = element as Label;
			if (label == null)
				throw new ArgumentException("Element must be of type Label");

			Element = label;
			_motionEventHelper.UpdateElement(element);
		}

		void IVisualElementRenderer.SetLabelFor(int? id)
		{
			if (_defaultLabelFor == null)
				_defaultLabelFor = ViewCompat.GetLabelFor(this);

			ViewCompat.SetLabelFor(this, (int)(id ?? _defaultLabelFor));
		}

		void IVisualElementRenderer.UpdateLayout()
		{
			VisualElementTracker tracker = _visualElementTracker;
			tracker?.UpdateLayout();
		}

View on GitHub (pinned to f377ff1c5e)

Solutions

  1. Register LabelRenderer (or subclass) only for Label or Label-derived types.
  2. Avoid null element arguments — the renderer does not distinguish null from wrong type.
  3. If subclassing for a control that does not extend Label, choose a different base renderer.

Example fix

// before
var label = element as Label;
if (label == null)
    throw new ArgumentException("Element must be of type Label");
// after — explicit null guard
if (element == null)
    throw new ArgumentNullException(nameof(element));
if (element is not Label label)
    throw new ArgumentException("Element must be of type Label", nameof(element));
Defensive patterns

Strategy: validation

Validate before calling

if (element is null) throw new ArgumentNullException(nameof(element));
if (element is not Label) throw new ArgumentException("Expected Label.", nameof(element));
((IVisualElementRenderer)renderer).SetElement(element);

Type guard

static bool IsLabel(VisualElement e) => e is Label;

Prevention

When it happens

Trigger: SetElement called with a non-Label element, or null. The check is `label == null`, which catches both non-Label and null inputs.

Common situations: Wrong ExportRenderer registration; a custom LabelRenderer subclass mapped to a non-Label control; passing null during a teardown/swap sequence.

Related errors


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