dotnet/wpf · error · InvalidOperationException

SR.NotAllowedBeforeShow

Error message

SR.NotAllowedBeforeShow

What it means

Window.VerifyHwndCreateShowState throws InvalidOperationException(SR.NotAllowedBeforeShow) when HwndCreatedButNotShown is true — i.e. the window's Win32 HWND has been created but the window has never actually been shown yet. WPF throws because the invoked operation is only meaningful on a window that has been displayed (it has a real, shown native window and position/state).

Solutions

  1. Call window.Show() or window.ShowDialog() before invoking operations that require a shown window.
  2. Move state queries/changes into the window's Loaded or ContentRendered event, which fire after the window is shown.
  3. If using Hide()/re-show patterns, verify IsLoaded is true before manipulating the window.

Example fix

// before
var w = new MyWindow();
w.Restore();

// after
var w = new MyWindow();
w.Show();
w.Restore();
Defensive patterns

Strategy: validation

Validate before calling

if (!window.IsLoaded) { window.Show(); }

Type guard

bool IsShown(Window w) => w != null && w.IsLoaded && w.IsVisible;

Try / catch

try { w.Restore(); }
catch (InvalidOperationException ex) when (ex.Message.Contains("before") || ex.Message.Contains("NotAllowedBeforeShow"))
{ w.Show(); w.Restore(); }

Prevention

When it happens

Trigger: Calling public Window members that require a shown window (e.g. WindowStartupLocation/state transitions, certain Restore/Focus operations validated by this helper) while the HWND was created but Show()/ShowDialog() has not completed.

Common situations: Interacting with a window between construction and Show(); creating a window and immediately querying/mutating window state before the show call; Show() failing silently so the window stays in created-but-not-shown state.

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/2921eb175775246c. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Window.cs:3714

        private void VerifyNotClosing()
        {
            if (_isClosing)
            {
                throw new InvalidOperationException(SR.InvalidOperationDuringClosing);
            }

            if (!IsSourceWindowNull && IsCompositionTargetInvalid)
            {
                throw new InvalidOperationException(SR.InvalidCompositionTarget);
            }
        }

        private void VerifyHwndCreateShowState()
        {
            if (HwndCreatedButNotShown)
            {
                throw new InvalidOperationException(SR.NotAllowedBeforeShow);
            }
        }

        /// <summary>
        ///     sets the IWindowService attached property
        /// </summary>
        private void SetIWindowService()
        {
            if (GetValue(IWindowServiceProperty) == null)
            {
                SetValue(IWindowServiceProperty, (IWindowService)this);
            }
        }

        private IntPtr GetCurrentMonitorFromMousePosition()
        {
            // center on the screen on which the mouse is on
            NativeMethods.POINT pt = default;

View on GitHub (pinned to 81131a70a4)