AvaloniaUI/Avalonia · error · ArgumentOutOfRangeException

'MaxTextHeight' property value cannot be NaN.

Error message

'MaxTextHeight' property value cannot be NaN.

What it means

MaxTextHeight additionally rejects NaN. Because NaN contaminates any comparison (NaN > 0 is false, NaN <= 0 is false), the prior guard would not catch it; this explicit check ensures the field never stores a non-numeric value.

Source

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

        /// <summary>
        /// Sets the maximum length of a column of text.
        /// The last line of text displayed is the last whole line that will fit within this limit,
        /// or the nth line as specified by MaxLineCount, whichever occurs first.
        /// Use the Trimming property to control how the omission of text is indicated.
        /// </summary>
        public double MaxTextHeight
        {
            set
            {
                if (value <= 0)
                {
                    throw new ArgumentOutOfRangeException(nameof(value), $"'{nameof(MaxTextHeight)}' property value must be greater than zero.");
                }

                if (double.IsNaN(value))
                {
                    throw new ArgumentOutOfRangeException(nameof(value), $"'{nameof(MaxTextHeight)}' property value cannot be NaN.");
                }

                _maxTextHeight = value;

                InvalidateMetrics();
            }
            get
            {
                return _maxTextHeight;
            }
        }

        /// <summary>
        /// Defines the maximum number of lines to display.
        /// The last line of text displayed is the lineCount-1'th line,
        /// or the last whole line that will fit within the count set by MaxTextHeight,
        /// whichever occurs first.
        /// Use the Trimming property to control how the omission of text is indicated

View on GitHub (pinned to 11c5427268)

Solutions

  1. Filter with double.IsFinite(value) before assigning; substitute a positive default for non-finite values.
  2. Fix the upstream computation producing NaN.
  3. Use a nullable double in the model so 'unset' is distinct from 'set to NaN'.
  4. Add unit tests covering NaN/Infinity edge cases.

Example fix

// before
ft.MaxTextHeight = computed; // computed may be NaN

// after
ft.MaxTextHeight = double.IsFinite(computed) && computed > 0
    ? computed
    : double.PositiveInfinity;
Defensive patterns

Strategy: validation

Validate before calling

if (double.IsFinite(value) && value > 0) ft.MaxTextHeight = value;
else /* leave unset / use no limit */ ;

Prevention

When it happens

Trigger: Binding MaxTextHeight to a computation that yields 0/0; deserialization producing NaN from a missing field; a zoom factor of Infinity multiplied by 0.

Common situations: View-model division by zero feeding MaxTextHeight; XAML converter returning NaN for an unset value; animation interpolation crossing a NaN.

Related errors


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