dotnet/wpf · error · ArgumentException

SR.InvalidEmptyArray

Error message

SR.InvalidEmptyArray

What it means

The gestureRecognitionResults parameter of InkCanvasGestureEventArgs must yield at least one GestureRecognitionResult. After copying the IEnumerable into a list, a Count == 0 check throws ArgumentException (SR.InvalidEmptyArray) because the event must carry at least one recognition result.

Solutions

  1. Only construct InkCanvasGestureEventArgs when the recognizer produced at least one result; otherwise take the 'no gesture' code path.
  2. If recognition genuinely found nothing, add a fallback GestureRecognitionResult (e.g., ApplicationGesture.NoGesture) instead of an empty list.
  3. Materialize and check results.Count before calling the constructor.

Example fix

// before
var args = new InkCanvasGestureEventArgs(strokes, recognizerResults);

// after
var results = recognizerResults.ToList();
if (results.Count == 0)
    results.Add(new GestureRecognitionResult(0.0, ApplicationGesture.NoGesture));
var args = new InkCanvasGestureEventArgs(strokes, results);
Defensive patterns

Strategy: validation

Validate before calling

var results = gestureRecognitionResults?.ToList();
if (results == null || results.Count == 0)
    return; // no recognition results: take no-gesture path
var args = new InkCanvasGestureEventArgs(strokes, results);

Type guard

bool HasRecognitionResults(IEnumerable<GestureRecognitionResult> r) => r != null && r.Any();

Try / catch

try { var args = new InkCanvasGestureEventArgs(strokes, results); }
catch (ArgumentException ex) when (ex.ParamName == "gestureRecognitionResults")
{ /* recognizer produced nothing; treat as no gesture */ }

Prevention

When it happens

Trigger: new InkCanvasGestureEventArgs(strokes, Array.Empty<GestureRecognitionResult>()), an empty results list from a recognizer that found no candidates, or a lazy IEnumerable that enumerates to zero items.

Common situations: Wrapping a gesture recognizer that returns no matches without special-casing the 'no gesture' outcome; passing the result of a LINQ query that filtered everything out; test fixtures with stub recognizers returning empty lists.

Understand the failure class

Background: "must not be empty", "cannot be empty" — required-field validation errors across open-source libraries — this error's family across 41 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Ink/Events.cs:312

        /// <summary>
        /// TBD
        /// </summary>
        /// <param name="strokes">strokes</param>
        /// <param name="gestureRecognitionResults">gestureRecognitionResults</param>
        public InkCanvasGestureEventArgs(StrokeCollection strokes, IEnumerable<GestureRecognitionResult> gestureRecognitionResults)
            : base(InkCanvas.GestureEvent)
        {
            ArgumentNullException.ThrowIfNull(strokes);
            if (strokes.Count < 1)
            {
                throw new ArgumentException(SR.InvalidEmptyStrokeCollection, nameof(strokes));
            }
            ArgumentNullException.ThrowIfNull(gestureRecognitionResults);
            List<GestureRecognitionResult> results = 
                new List<GestureRecognitionResult>(gestureRecognitionResults);
            if (results.Count == 0)
            {
                throw new ArgumentException(SR.InvalidEmptyArray, nameof(gestureRecognitionResults));
            }
            _strokes = strokes;
            _gestureRecognitionResults = results;
        }

        /// <summary>
        /// TBD
        /// </summary>
        public StrokeCollection Strokes
        {
            get { return _strokes; }
        }

        /// <summary>
        /// TBD
        /// </summary>
        /// <returns></returns>
        public ReadOnlyCollection<GestureRecognitionResult> GetGestureRecognitionResults()

View on GitHub (pinned to 81131a70a4)