dotnet/wpf · error · InvalidEnumArgumentException

SR.Format(SR.Enum_Invalid, nameof(systemGesture))

Error message

SR.Format(SR.Enum_Invalid, nameof(systemGesture))

What it means

RawStylusSystemGestureInputReport.Initialize validates that the SystemGesture is one of the recognized gestures recognized by IsValidSystemGesture (with the two boolean gates). An unrecognized gesture throws InvalidEnumArgumentException with SR.Enum_Invalid.

Solutions

  1. Pass only supported SystemGesture values (Tap, DoubleTap, RightTap, Drag, RightDrag, HoldEnter, HoldLeave, HoverEnter, HoverLeave, Flick, TwoFingerTap, AllGestures)
  2. Validate the gesture before constructing the report (replicate the IsValidSystemGesture check)
  3. Map foreign gesture IDs to WPF SystemGesture values explicitly instead of casting raw ints

Example fix

// before
var report = new RawStylusSystemGestureInputReport(mode, ts, src, (SystemGesture)0x55, x, y, btn);
// after
var gesture = (SystemGesture)0x55;
if (!Enum.IsDefined(gesture)) throw new ArgumentException(nameof(systemGesture));
var report = new RawStylusSystemGestureInputReport(mode, ts, src, gesture, x, y, btn);
Defensive patterns

Strategy: validation

Validate before calling

if (!Enum.IsDefined(typeof(SystemGesture), gesture)) throw new ArgumentException(nameof(gesture));

Try / catch

try { new RawStylusSystemGestureInputReport(...); } catch (InvalidEnumArgumentException) { /* map/normalize gesture id */ }

Prevention

When it happens

Trigger: Constructing a RawStylusSystemGestureInputReport with a systemGesture value that fails IsValidSystemGesture(gesture, true, true) — i.e. not one of the supported gestures accepted under those constraints (e.g. (SystemGesture)99, or a gesture excluded by the gate flags such as a two-finger/tap-only gesture where not permitted).

Common situations: Custom pen input providers or test fakes synthesizing system-gesture reports with arbitrary integer gestures; porting from WinTab/REAL-time stylus where the gesture id spaces differ.

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/9ac1957dacd295ba. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Stylus/Common/RawStylusSystemGestureInputReport.cs:157

            int timestamp,
            PresentationSource inputSource,
            PenContext penContext,
            int tabletId,
            int stylusDeviceId,
            SystemGesture systemGesture,
            int gestureX,
            int gestureY,
            int buttonState)
            : base(mode, timestamp, inputSource, penContext, RawStylusActions.SystemGesture, tabletId, stylusDeviceId, Array.Empty<int>())
        {
            Initialize(systemGesture, gestureX, gestureY, buttonState);
        }

        private void Initialize(SystemGesture systemGesture, int gestureX, int gestureY, int buttonState)
        {
            if (!RawStylusSystemGestureInputReport.IsValidSystemGesture(systemGesture, true, true))
            {
                throw new InvalidEnumArgumentException(SR.Format(SR.Enum_Invalid, nameof(systemGesture)));
            }

            _id = systemGesture;
            _gestureX = gestureX;
            _gestureY = gestureY;
            _buttonState = buttonState;
        }

        /////////////////////////////////////////////////////////////////////
        /// <summary>
        ///     Read-only access to the stylus gesture id.
        /// </summary>
        internal SystemGesture SystemGesture { get { return _id; } }

        /////////////////////////////////////////////////////////////////////
        /// <summary>
        ///     Read-only access to the X location of the system gesture
        ///      in tablet device coordinates.

View on GitHub (pinned to 81131a70a4)