dotnet/wpf · error · InvalidEnumArgumentException

InvalidEnumArgumentException("action", (int)action…

Error message

InvalidEnumArgumentException("action", (int)action, typeof(RawUIStateActions))

What it means

The RawUIStateInputReport constructor validates the action parameter and throws InvalidEnumArgumentException when it is not a defined RawUIStateActions member. RawUIStateActions describes keyboard focus/捕捉 UI-state operations, and the constructor rejects undefined values before storing them.

Solutions

  1. Pass only defined RawUIStateActions members (use named values, not numeric casts).
  2. Pre-validate with Enum.IsDefined(typeof(RawUIStateActions), action).
  3. Fix deserialization to map legacy integers to current enum members.
  4. Compile against the same WPF version used at runtime to avoid enum drift.

Example fix

// before
var report = new RawUIStateInputReport(src, mode, ts, (RawUIStateActions)n, targets);

// after
var action = (RawUIStateActions)n;
if (!IsValidRawUIStateAction(action))
    action = RawUIStateActions.SetFocus; // map to a defined value
var report = new RawUIStateInputReport(src, mode, ts, action, targets);
Defensive patterns

Strategy: validation

Validate before calling

if (!Enum.IsDefined(typeof(RawUIStateActions), action))
    action = RawUIStateActions.SetFocus; // substitute a defined value

Type guard

bool TryGetUIStateAction(int raw, out RawUIStateActions action)
{
    action = (RawUIStateActions)raw;
    return Enum.IsDefined(typeof(RawUIStateActions), action);
}

Try / catch

try
{
    var report = new RawUIStateInputReport(src, mode, ts, action, targets);
}
catch (InvalidEnumArgumentException ex)
{
    Log($"Invalid RawUIStateActions: {ex}");
}

Prevention

When it happens

Trigger: Constructing RawUIStateInputReport with an undefined RawUIStateActions value — e.g. casting an int from persisted state, passing 0, or using flags that are not one of the defined actions.

Common situations: Custom input-report synthesis in automation or accessibility tooling; deserializing old/foreign state files into the enum; typos when hand-writing enum combinations.

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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/RawUIStateInputReport.cs:38

        /// </param>
        /// <param name="timestamp">
        ///     The time when the input occurred.
        /// </param>
        /// <param name="action">
        ///     The action being reported.
        /// </param>
        /// <param name="targets">
        ///     The targets being reported.
        /// </param>
        public RawUIStateInputReport(
            PresentationSource inputSource,
            InputMode mode,
            int timestamp,
            RawUIStateActions action,
            RawUIStateTargets targets) : base(inputSource, InputType.Keyboard, mode, timestamp)
        {
            if (!IsValidRawUIStateAction(action))
                throw new System.ComponentModel.InvalidEnumArgumentException("action", (int)action, typeof(RawUIStateActions));
            if (!IsValidRawUIStateTargets(targets))
                throw new System.ComponentModel.InvalidEnumArgumentException("targets", (int)targets, typeof(RawUIStateTargets));

            _action = action;
            _targets = targets;
        }

        /// <summary>
        ///     Read-only access to the action that was reported.
        /// </summary>
        public RawUIStateActions Action {get {return _action;}}

        /// <summary>
        ///     Read-only access to the targets that were reported.
        /// </summary>
        public RawUIStateTargets Targets {get {return _targets;}}

        // IsValid Method for RawUIStateActions.

View on GitHub (pinned to 81131a70a4)