AvaloniaUI/Avalonia · error · ArgumentOutOfRangeException

The parameter value must be greater than zero.

Error message

The parameter value must be greater than zero.

What it means

FormattedText.ValidateFontSize rejects a non-positive emSize because font metrics and layout math divide by em size and produce nonsensical metrics at or below zero. The guard runs in the FormattedText constructor before any layout work.

Source

Thrown at src/Avalonia.Base/Media/FormattedText.cs:104

                flowDirection,
                TextAlignment.Left,
                false,
                false,
                runProps,
                TextWrapping.WrapWithOverflow,
                0, // line height not specified
                0, // indentation not specified
                0
            );

            InvalidateMetrics();
        }

        private static void ValidateFontSize(double emSize)
        {
            if (emSize <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(emSize), "The parameter value must be greater than zero.");
            }

            if (emSize > MaxFontEmSize)
            {
                throw new ArgumentOutOfRangeException(nameof(emSize), $"The parameter value cannot be greater than '{MaxFontEmSize}'");
            }

            if (double.IsNaN(emSize))
            {
                throw new ArgumentOutOfRangeException(nameof(emSize), "The parameter value must be a number.");
            }
        }

        private static void ValidateFlowDirection(FlowDirection flowDirection, string parameterName)
        {
            if ((int)flowDirection < 0 || (int)flowDirection > (int)FlowDirection.RightToLeft)
            {
                throw new InvalidEnumArgumentException(parameterName, (int)flowDirection, typeof(FlowDirection));

View on GitHub (pinned to 11c5427268)

Solutions

  1. Clamp the user-facing font size to a small positive minimum (e.g. 1.0) before constructing FormattedText.
  2. Validate bound input in the view-model setter and reject/coerce values <= 0.
  3. Use double.IsFinite checks upstream to also catch NaN/Infinity.
  4. Set a sensible default emSize (e.g. the system UI font size) so uninitialized fields do not hit the API with 0.

Example fix

// before
var ft = new FormattedText(text, fontFamily, requestedSize, ...); // requestedSize may be 0

// after
var safeSize = double.IsFinite(requestedSize) && requestedSize > 0 ? requestedSize : 12.0;
var ft = new FormattedText(text, fontFamily, safeSize, ...);
Defensive patterns

Strategy: validation

Validate before calling

static double CoerceFontSize(double v) => double.IsFinite(v) && v > 0 ? v : 12.0;

Prevention

When it happens

Trigger: Constructing FormattedText with FontSize bound from a Slider whose Minimum is not clamped at 0; binding FontSize to a calculated value that can go to 0 or negative on edge cases; passing a default double (0) from an uninitialized field.

Common situations: A XAML binding where the user can drag font size to zero; default(double) being passed because a property initializer was skipped; animating FontSize to 0 as a 'fade' trick.

Related errors


AI-assisted analysis of AvaloniaUI/Avalonia@11c5427268 (2026-08-13). Data as JSON: /api/errors/00587274249bd0b3. Report an issue: GitHub.