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);
                }
                finally

View on GitHub (pinned to 11c5427268)

Solutions

  1. Replace NaN dimensions with 0 or a concrete value, or use double.PositiveInfinity for unconstrained axes, before calling Measure.
  2. Trace the source of the NaN — inspect bindings and computed sizes feeding into the Measure call.
  3. 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

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


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