dotnet/maui · error · ArgumentException

Element must be of type ProgressBar.

Error message

Element must be of type ProgressBar.

What it means

MaterialProgressBarRenderer renders Material Design ProgressBar elements on Android. SetElement casts the VisualElement to ProgressBar and throws ArgumentException if the element is not a ProgressBar. The renderer reads Element.Progress and Element.ProgressColor, which only exist on ProgressBar.

Source

Thrown at src/Compatibility/Material/src/Android/MaterialProgressBarRenderer.cs:143

				UpdateColors();
		}

		void UpdateColors()
		{
			if (Element == null || Control == null)
				return;

			this.ApplyProgressBarColors(Element.ProgressColor, Element.BackgroundColor);
		}

		void UpdateProgress() => Control.Progress = (int)(Element.Progress * MaximumValue);

		// IVisualElementRenderer
		VisualElement IVisualElementRenderer.Element => Element;
		VisualElementTracker IVisualElementRenderer.Tracker => _visualElementTracker;
		AView IVisualElementRenderer.View => this;
		void IVisualElementRenderer.SetElement(VisualElement element) =>
			Element = (element as ProgressBar) ?? throw new ArgumentException("Element must be of type ProgressBar.");
		void IVisualElementRenderer.UpdateLayout() =>
			_visualElementTracker?.UpdateLayout();

		SizeRequest IVisualElementRenderer.GetDesiredSize(int widthConstraint, int heightConstraint)
		{
			Measure(widthConstraint, heightConstraint);
			return new SizeRequest(new Size(Control.MeasuredWidth, Context.ToPixels(4)), new Size(Context.ToPixels(4), Context.ToPixels(4)));
		}

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

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

		// IViewRenderer

View on GitHub (pinned to f377ff1c5e)

Solutions

  1. Verify ExportRenderer maps ProgressBar (not ActivityIndicator) to MaterialProgressBarRenderer.
  2. Ensure custom progress controls derive from ProgressBar if they use this renderer.
  3. Check for duplicate ProgressBar registrations across assemblies.
  4. Review the `VisualMarker.Material` application targets.

Example fix

// before
[assembly: ExportRenderer(typeof(ActivityIndicator), typeof(MaterialProgressBarRenderer))]

// after
[assembly: ExportRenderer(typeof(ProgressBar), typeof(MaterialProgressBarRenderer))]
Defensive patterns

Strategy: type-guard

Validate before calling

if (element is not ProgressBar)
    throw new InvalidOperationException($"Expected ProgressBar, got {element?.GetType().Name}");
renderer.SetElement(element);

Type guard

static bool IsProgressBar(VisualElement? element) => element is ProgressBar;

Prevention

When it happens

Trigger: A non-ProgressBar element is dispatched to MaterialProgressBarRenderer via a misconfigured registration. Common when ActivityIndicator and ProgressBar renderers are confused due to similar naming.

Common situations: Swapping ActivityIndicator and ProgressBar renderer registrations. Registering a custom progress-like control that does not derive from ProgressBar. MAUI migration mismatches.

Related errors


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