AvaloniaUI/Avalonia · error · InvalidOperationException
Cannot call Measure using a size with NaN values.
Error message
Cannot call Measure using a size with NaN values.
What it means
Measure throws when either Width or Height of the supplied Size is NaN. Avalonia's layout contract treats NaN as 'unset/undefined' for layout slots and explicitly forbids passing it as available size, because NaN cannot be compared or used in measure math. Infinity is allowed (means 'unconstrained'), but NaN is always rejected.
Source
Thrown at src/Avalonia.Base/Layout/Layoutable.cs:371
/// </summary>
internal Rect? PreviousArrange => _previousArrange;
/// <summary>
/// Creates the visual children of the control, if necessary
/// </summary>
public virtual void ApplyTemplate()
{
}
/// <summary>
/// Carries out a measure of the control.
/// </summary>
/// <param name="availableSize">The available size for the control.</param>
public void Measure(Size availableSize)
{
if (double.IsNaN(availableSize.Width) || double.IsNaN(availableSize.Height))
{
throw new InvalidOperationException("Cannot call Measure using a size with NaN values.");
}
if (!IsMeasureValid || _previousMeasure != availableSize)
{
using var activity = Diagnostic.MeasuringLayoutable()?
.AddTag(Diagnostic.Tags.Control, this);
var previousDesiredSize = DesiredSize;
var desiredSize = default(Size);
IsMeasureValid = true;
try
{
_measuring = true;
desiredSize = MeasureCore(availableSize);
}
finallyView on GitHub (pinned to 11c5427268)
Solutions
- Replace NaN dimensions with 0 or a concrete value, or use double.PositiveInfinity for unconstrained axes, before calling Measure.
- Trace the source of the NaN — inspect bindings and computed sizes feeding into the Measure call.
- In custom panels, sanitize availableSize: size = new Size(double.IsNaN(size.Width) ? 0 : size.Width, ...).
Example fix
// before control.Measure(new Size(double.NaN, availableHeight)); // after var w = double.IsNaN(availableWidth) ? 0 : availableWidth; control.Measure(new Size(w, availableHeight));
Defensive patterns
Strategy: validation
Validate before calling
static Size Sanitize(Size s) => new Size(
double.IsNaN(s.Width) ? 0 : s.Width,
double.IsNaN(s.Height) ? 0 : s.Height);
control.Measure(Sanitize(availableSize)); Prevention
- In custom panels, sanitize availableSize before forwarding to child.Measure.
- Never pass DesiredSize or computed values that could be NaN into Measure.
- Treat NaN as a bug in upstream binding/computation and trace it.
When it happens
Trigger: Calling control.Measure(availableSize) where availableSize.Width or availableSize.Height is double.NaN. Common when the size is sourced from an uninitialized property, an unset binding, or computed arithmetic that produced NaN (e.g. 0/0).
Common situations: Binding AvailableSize to a property that defaults to NaN. Computation like division by zero producing NaN before Measure. Passing DesiredSize (which can legitimately be NaN in edge states) back as available size. Custom layout panels computing available space incorrectly.
Related errors
- Invalid Arrange rectangle.
- Invalid size returned for Measure.
- 'MaxTextHeight' property value cannot be NaN.
- The control isn't currently attached to a toplevel
- Invalid render scaling value {scaling}
AI-assisted analysis of AvaloniaUI/Avalonia@11c5427268 (2026-08-13).
Data as JSON: /api/errors/68205d962302c423.
Report an issue: GitHub.