AvaloniaUI/Avalonia · error · ArgumentOutOfRangeException
The parameter value must be a number.
Error message
The parameter value must be a number.
What it means
ValidateFontSize rejects NaN because NaN propagates through every arithmetic operation and silently corrupts all subsequent layout metrics (line heights, advance widths, hit-testing rectangles). NaN cannot produce a valid glyph layout.
Source
Thrown at src/Avalonia.Base/Media/FormattedText.cs:114
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));
}
}
private int ValidateRange(int startIndex, int count)
{
if (startIndex < 0 || startIndex > _text.Length)
{
throw new ArgumentOutOfRangeException(nameof(startIndex));
}
View on GitHub (pinned to 11c5427268)
Solutions
- Filter with double.IsFinite(emSize) before passing to FormattedText; substitute a sensible default for non-finite values.
- Fix the upstream computation producing NaN (guard divisors, check for 0/0).
- Add a numeric IValueConverter that maps NaN/Infinity to a fallback font size.
- Validate deserialized font sizes against a schema/range and reject missing fields.
Example fix
// before var emSize = (a / b) * scale; // b may be 0 -> NaN propagates var ft = new FormattedText(text, font, emSize, ...); // after var raw = (a / Math.Max(b, 1)) * scale; var emSize = double.IsFinite(raw) ? raw : 12.0; var ft = new FormattedText(text, font, emSize, ...);
Defensive patterns
Strategy: validation
Validate before calling
double em = double.IsFinite(requested) ? requested : 12.0; em = em > 0 ? em : 12.0;
Prevention
- Filter all numeric inputs with double.IsFinite.
- Guard divisors that feed font-size calculations.
- Map missing deserialized numerics to a default, not NaN.
When it happens
Trigger: Binding FontSize to 0.0/0.0 or any computation that yields NaN; reading FontSize from a deserialized source where the value was missing and defaulted to NaN; a multiplication of Infinity * 0 in a zoom calculation.
Common situations: A division by zero in a view-model that feeds FontSize; JSON deserialization of a missing numeric field returning NaN on some serializers; XAML binding to a non-numeric source converted by a faulty IValueConverter.
Related errors
- The parameter value must be greater than zero.
- The parameter value cannot be greater than '{MaxFontEmSize}'
- 'MaxTextHeight' property value cannot be NaN.
- Parameter must be greater than or equal to zero.
- 'MaxTextHeight' property value must be greater than zero.
AI-assisted analysis of AvaloniaUI/Avalonia@11c5427268 (2026-08-13).
Data as JSON: /api/errors/058bdcdaa863c7a3.
Report an issue: GitHub.