dotnet/wpf · error · ArgumentOutOfRangeException

SR.IDPNegativePageNumber

Error message

SR.IDPNegativePageNumber

What it means

FlowDocumentPaginator.GetPageAsync throws ArgumentOutOfRangeException(SR.IDPNegativePageNumber) when the pageNumber argument is less than zero. Page numbers in the WPF pagination API are zero-based non-negative integers; a negative index has no meaning so the method validates and throws immediately.

Solutions

  1. Validate pageNumber >= 0 before calling GetPageAsync and clamp or return early.
  2. Check the source of the page number; replace -1 'unknown' sentinels with nullable state.
  3. Ensure pagination loops start at 0 and stay below PageCount.
  4. If a content-position lookup failed, do not pass the sentinel to GetPageAsync; handle the failure instead.

Example fix

// before
paginator.GetPageAsync(pageIndex - 1, null); // pageIndex==0 → -1
// after
if (pageIndex - 1 >= 0) {
    paginator.GetPageAsync(pageIndex - 1, null);
}
Defensive patterns

Strategy: validation

Validate before calling

if (pageNumber < 0)
    throw new ArgumentOutOfRangeException(nameof(pageNumber));
paginator.GetPageAsync(pageNumber, userState);

Type guard

bool IsValidPageNumber(int n) => n >= 0;

Try / catch

try { paginator.GetPageAsync(n, state); }
catch (ArgumentOutOfRangeException ex) when (ex.ParamName == "pageNumber") {
    // log and clamp: n = Math.Max(0, n);
}

Prevention

When it happens

Trigger: Calling GetPageAsync(-1, userState) or any negative page number — commonly from code computing page indices with uninitialized variables, off-by-one loops (for i = -1), or result of a failed lookup returning -1 passed straight through.

Common situations: Loop bounds computed as page-1 with page=0; caching code storing -1 for 'unknown page' and passing it to GetPageAsync; content position lookups (GetPageNumber) returning sentinel values that are fed to GetPageAsync.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/documents/FlowDocumentPaginator.cs:73

        //-------------------------------------------------------------------

        #region Public Methods


        /// <summary>
        /// Async version of <see cref="DocumentPaginator.GetPage"/>
        /// </summary>
        /// <param name="pageNumber">Page number.</param>
        /// <param name="userState">Unique identifier for the asynchronous task.</param>
        /// <exception cref="ArgumentOutOfRangeException">
        /// Throws ArgumentOutOfRangeException if PageNumber is negative.
        /// </exception>
        public override void GetPageAsync(int pageNumber, object userState)
        {
            // Page number cannot be negative.
            if (pageNumber < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(pageNumber), SR.IDPNegativePageNumber);
            }

            // Reentrancy check.
            if (_document.StructuralCache.IsFormattingInProgress)
            {
                throw new InvalidOperationException(SR.FlowDocumentFormattingReentrancy);
            }
            if (_document.StructuralCache.IsContentChangeInProgress)
            {
                throw new InvalidOperationException(SR.TextContainerChangingReentrancyInvalid);
            }

            DocumentPage page = null;

            if (!_backgroundPagination)
            {
                page = GetPage(pageNumber);
            }

View on GitHub (pinned to 81131a70a4)