dotnet/wpf · error · ArgumentException

SR.Format(SR.InvalidCtorParameterNoNaN…

Error message

SR.Format(SR.InvalidCtorParameterNoNaN, "cacheAfterViewport")

What it means

The two-parameter VirtualizationCacheLength constructor throws ArgumentException (InvalidCtorParameterNoNaN with "cacheAfterViewport") when cacheAfterViewport is NaN. Both cache extents must be finite values.

Solutions

  1. Check double.IsNaN(cacheAfterViewport) before constructing and use a default such as 0.
  2. Fix the upstream calculation that produced NaN.
  3. Centralize construction in a helper that sanitizes both parameters.

Example fix

// before
var len = new VirtualizationCacheLength(1, computedCacheAfter);
// after
var len = new VirtualizationCacheLength(1, double.IsNaN(computedCacheAfter) ? 0 : computedCacheAfter);
Defensive patterns

Strategy: validation

Validate before calling

if (!double.IsNaN(cacheAfterViewport)) { var len = new VirtualizationCacheLength(cacheBeforeViewport, cacheAfterViewport); }

Type guard

bool IsValidCacheLength(double v) => !double.IsNaN(v);

Try / catch

try { var len = new VirtualizationCacheLength(before, after); } catch (ArgumentException) { var len = new VirtualizationCacheLength(before, 0); }

Prevention

When it happens

Trigger: Calling new VirtualizationCacheLength(x, double.NaN), or the value coming from parsing/computation that produced NaN (e.g. Converter output, division by zero).

Common situations: Setting VirtualizingPanel.CacheLength programmatically or via a converter where the cache-after value is computed and accidentally NaN.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/6cb0b78efc17573c. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/VirtualizationCacheLength.cs:73

        /// If <c>cacheBeforeAndAfterViewport</c> parameter is <c>double.NaN</c>
        /// or <c>cacheBeforeAndAfterViewport</c> parameter is <c>double.NegativeInfinity</c>
        /// or <c>cacheBeforeAndAfterViewport</c> parameter is <c>double.PositiveInfinity</c>.
        /// </exception>
        public VirtualizationCacheLength(double cacheBeforeAndAfterViewport)
            : this(cacheBeforeAndAfterViewport, cacheBeforeAndAfterViewport)
        {
        }

        public VirtualizationCacheLength(double cacheBeforeViewport, double cacheAfterViewport)
        {
            if (double.IsNaN(cacheBeforeViewport))
            {
                throw new ArgumentException(SR.Format(SR.InvalidCtorParameterNoNaN, "cacheBeforeViewport"));
            }

            if (double.IsNaN(cacheAfterViewport))
            {
                throw new ArgumentException(SR.Format(SR.InvalidCtorParameterNoNaN, "cacheAfterViewport"));
            }

            _cacheBeforeViewport = cacheBeforeViewport;
            _cacheAfterViewport = cacheAfterViewport;
        }

        #endregion Constructors

        //------------------------------------------------------
        //
        //  Public Methods
        //
        //------------------------------------------------------

        #region Public Methods

        /// <summary>
        /// Overloaded operator, compares 2 VirtualizationCacheLength's.

View on GitHub (pinned to 81131a70a4)