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
- Validate the value is a defined WindowVisualState member with Enum.IsDefined before calling SetVisualState.
- Fix the casting code to use the enum type end-to-end instead of int with unchecked casts.
- Catch ArgumentException and log the offending value to identify the source of the bad enum.
- 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
- Use the WindowVisualState enum type end-to-end; avoid int casts without Enum.IsDefined
- Map foreign API state values to WindowVisualState explicitly before interop calls
- Enable checked enum parsing at boundaries (parsing, COM, reflection)
- Never pass raw integers from Win32 APIs directly as WindowVisualState
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
- ' ' is not a valid value for this control.
- origin
- origin
- SR.AtLeastOnePropertyMustBeSpecified
- SR.Format(SR.NotAValidValue, str)
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)