dotnet/wpf · error · InvalidEnumArgumentException

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

Error message

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

What it means

The StylusSystemGestureEventArgs constructor validates the SystemGesture value with RawStylusSystemGestureInputReport.IsValidSystemGesture(gesture, false, false). If the gesture is not a valid system gesture, an InvalidEnumArgumentException with SR.Enum_Invalid for 'systemGesture' is thrown. This API is for framework-internal event construction.

Solutions

  1. Only pass defined SystemGesture members accepted by IsValidSystemGesture
  2. Validate the gesture value against Enum.IsDefined(typeof(SystemGesture), value) before constructing
  3. Fix integer-to-enum casts in custom input replay code

Example fix

// before
var args = new StylusSystemGestureEventArgs(device, ts, (SystemGesture)999);
// after
var gesture = (SystemGesture)999;
if (!Enum.IsDefined(typeof(SystemGesture), gesture)) throw new ArgumentException("bad gesture");
var args = new StylusSystemGestureEventArgs(device, ts, gesture);
Defensive patterns

Strategy: validation

Validate before calling

if (!Enum.IsDefined(typeof(SystemGesture), gesture)) throw new ArgumentException($"Invalid SystemGesture: {gesture}");

Type guard

static bool IsValidGesture(SystemGesture g) => Enum.IsDefined(typeof(SystemGesture), g);

Try / catch

try { var args = new StylusSystemGestureEventArgs(device, ts, gesture); }
catch (InvalidEnumArgumentException ex) { /* skip or sanitize the synthesized event */ }

Prevention

When it happens

Trigger: Constructing StylusSystemGestureEventArgs with a SystemGesture value outside the valid set (invalid cast, undefined enum member).

Common situations: Replaying or synthesizing stylus events from recorded input data with out-of-range ints cast to SystemGesture; custom input providers injecting gestures.

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/845f4bc416e65bce. Report an issue: GitHub.

Appendix: source

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

        ///     Initializes a new instance of the StylusSystemGestureEventArgs class.
        /// </summary>
        /// <param name="stylusDevice">
        ///     The logical Stylus device associated with this event.
        /// </param>
        /// <param name="timestamp">
        ///     The time when the input occured.
        /// </param>
        /// <param name="systemGesture"> 
        ///     The type of system gesture.
        /// </param>
        public StylusSystemGestureEventArgs(
            StylusDevice stylusDevice, int timestamp,
            SystemGesture systemGesture) :
            base(stylusDevice, timestamp)
        {
            if (!RawStylusSystemGestureInputReport.IsValidSystemGesture(systemGesture, false, false))
            {
                throw new InvalidEnumArgumentException(SR.Format(SR.Enum_Invalid, "systemGesture"));
            }
            
            _id        = systemGesture;
        }

        /////////////////////////////////////////////////////////////////////
        /// <summary>
        ///     Initializes a new instance of the StylusSystemGestureEventArgs class.
        /// </summary>
        /// <param name="stylusDevice">
        ///     The logical Stylus device associated with this event.
        /// </param>
        /// <param name="timestamp">
        ///     The time when the input occured.
        /// </param>
        /// <param name="systemGesture"> 
        ///     The type of system gesture.
        /// </param>

View on GitHub (pinned to 81131a70a4)