dotnet/wpf · warning · ArgumentException

SR.UnexpectedWindowState (state)

Error message

SR.UnexpectedWindowState (state)

What it means

SetVisualState only handles Normal, Minimized, and Maximized; any other WindowVisualState value (there are no other public values, so this is a defensive default) reaches the default branch, which fires Debug.Fail and throws ArgumentException(SR.UnexpectedWindowState, nameof(state)) at HwndProxyElementProvider.cs:376.

Solutions

  1. Validate the value is a defined WindowVisualState member with Enum.IsDefined before calling SetVisualState.
  2. Fix the casting code to use the enum type end-to-end instead of int with unchecked casts.
  3. Catch ArgumentException and log the offending value to identify the source of the bad enum.
  4. If interoperating, map the foreign state value to the closest supported WindowVisualState before the call.

Example fix

// before
var state = (WindowVisualState)rawValue; // rawValue may be out of range
windowPattern.SetVisualState(state);

// after
if (Enum.IsDefined(typeof(WindowVisualState), rawValue))
{
    windowPattern.SetVisualState((WindowVisualState)rawValue);
}
Defensive patterns

Strategy: validation

Validate before calling

if (!Enum.IsDefined(typeof(WindowVisualState), state)) throw new ArgumentOutOfRangeException(nameof(state), state, "Not a valid WindowVisualState");

Type guard

static bool IsValidWindowVisualState(object v) => v is int i && Enum.IsDefined(typeof(WindowVisualState), i);

Try / catch

try { windowPattern.SetVisualState(state); } catch (ArgumentException ex) { log($"Bad WindowVisualState value: {state}"); }

Prevention

When it happens

Trigger: Passing an undefined/out-of-range WindowVisualState cast or an invalid value obtained from interop/reflection into IWindowProvider.SetVisualState — the enum value is not one of Normal(0)/Minimized(1)/Maximized(2).

Common situations: Code that stores window state as int and casts without validation; binding layers or COM callers passing raw integers; passing WindowBroken/WindowMinimizedTray-like values from other Windows APIs into the UIA enum by mistake.

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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClient/MS/Internal/Automation/HwndProxyElementProvider.cs:376

                    if (((IWindowProvider)this).VisualState == WindowVisualState.Maximized)
                    {
                        return;
                    }

                    ClearMenuMode();

                    if (!Misc.PostMessage(_hwnd, UnsafeNativeMethods.WM_SYSCOMMAND, (IntPtr)UnsafeNativeMethods.SC_MAXIMIZE, IntPtr.Zero))
                    {
                        throw new InvalidOperationException(SR.OperationCannotBePerformed);
                    }

                    return;
                }

                default:
                {
                    Debug.Fail("unexpected switch() case:");
                    throw new ArgumentException(SR.UnexpectedWindowState,nameof(state));
                }

            }

        }

        void IWindowProvider.Close()
        {
            ClearMenuMode();

            if (!Misc.PostMessage(_hwnd, UnsafeNativeMethods.WM_SYSCOMMAND, (IntPtr)UnsafeNativeMethods.SC_CLOSE, IntPtr.Zero))
            {
                throw new InvalidOperationException(SR.OperationCannotBePerformed);
            }
        }

        bool IWindowProvider.WaitForInputIdle( int milliseconds )
        {

View on GitHub (pinned to 81131a70a4)