dotnet/wpf · error · ArgumentException

The specified ApplicationGesture is not valid.

Error message

The specified ApplicationGesture is not valid.

What it means

GetApplicationGestureArrayAndVerify validates each element of the ApplicationGesture array with ApplicationGestureHelper.IsDefined before enabling it. Any value outside the defined enum range — such as raw numeric values or uninitialized enums — throws ArgumentException (SR.ApplicationGestureIsInvalid).

Solutions

  1. Filter entries with ApplicationGestureHelper.IsDefined(gesture) before assigning
  2. Sanitize persisted/config values and skip or fix undefined enum values
  3. Use only enum members compiled into ApplicationGesture; never cast unchecked ints
  4. Log and drop invalid values instead of passing them through

Example fix

// before
recognizer.EnabledGestures = loadedGestures; // from persisted ints
// after
recognizer.EnabledGestures = loadedGestures.Where(ApplicationGestureHelper.IsDefined).ToArray();
Defensive patterns

Strategy: validation

Validate before calling

if (gestures.Any(g => !ApplicationGestureHelper.IsDefined(g)))
    throw new ArgumentException("Contains undefined ApplicationGesture values", nameof(gestures));

Type guard

static bool AllDefined(ApplicationGesture[] g) => g.All(ApplicationGestureHelper.IsDefined);

Try / catch

try { recognizer.EnabledGestures = gestures; }
catch (ArgumentException) { recognizer.EnabledGestures = new[]{ ApplicationGesture.AllGestures }; }

Prevention

When it happens

Trigger: Assigning EnabledGestures containing an ApplicationGesture value not defined by the enum (e.g. cast from an invalid int, or a deserialized/garbage value).

Common situations: Restoring gesture settings from persisted config or user data that stored raw integers; interop code casting foreign enums into ApplicationGesture.

Understand the failure class

Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/Ink/GestureRecognizer/NativeRecognizer.cs:218

                {
                    count++;
                }
            }

            // Cannot be empty
            if (count == 0)
            {
                // An empty array is not allowed.
                throw new ArgumentException(SR.ApplicationGestureArrayLengthIsZero, nameof(applicationGestures));
            }

            bool foundAllGestures = false;
            List<ApplicationGesture> gestures = new List<ApplicationGesture>();
            foreach (ApplicationGesture gesture in applicationGestures)
            {
                if (!ApplicationGestureHelper.IsDefined(gesture))
                {
                    throw new ArgumentException(SR.ApplicationGestureIsInvalid, nameof(applicationGestures));
                }

                //check for allgestures
                if (gesture == ApplicationGesture.AllGestures)
                {
                    foundAllGestures = true;
                }

                //check for dupes
                if (gestures.Contains(gesture))
                {
                    throw new ArgumentException(SR.DuplicateApplicationGestureFound, nameof(applicationGestures));
                }

                gestures.Add(gesture);
            }

            // AllGesture cannot be specified with other gestures

View on GitHub (pinned to 81131a70a4)