{"record":{"id":"a268b2ef931c1b4c","repo":"MahApps/MahApps.Metro","slug":"failed-to-set-the-window-state-for-window-from-t","errorCode":null,"errorMessage":"Failed to set the window state for {window} from the settings.","messagePattern":"Failed to set the window state for (.+?) from the settings\\.","errorType":"exception","errorClass":"MahAppsException","httpStatus":null,"severity":"error","filePath":"src/MahApps.Metro/Behaviors/WindowsSettingBehavior.cs","lineNumber":146,"sourceCode":"            {\n                Trace.TraceError($\"{this}: The settings for {window} could not be reloaded! {e}\");\n                return;\n            }\n\n            // check for existing placement and prevent empty bounds\n            if (settings.Placement is null || settings.Placement.normalPosition.IsEmpty)\n            {\n                return;\n            }\n\n            try\n            {\n                var wp = settings.Placement.ToWINDOWPLACEMENT();\n                WinApiHelper.SetWindowPlacement(window, wp);\n            }\n            catch (Exception ex)\n            {\n                throw new MahAppsException($\"Failed to set the window state for {window} from the settings.\", ex);\n            }\n        }\n\n        private void SaveWindowState()\n        {\n            var window = this.AssociatedObject;\n            if (window is null)\n            {\n                return;\n            }\n\n            var settings = window.GetWindowPlacementSettings();\n            if (settings is null || !window.SaveWindowPosition)\n            {\n                return;\n            }\n\n            var windowHandle = new WindowInteropHelper(window).EnsureHandle();","sourceCodeStart":128,"sourceCodeEnd":164,"githubUrl":"https://github.com/MahApps/MahApps.Metro/blob/72099e310bac2d12ac98fd7560b69679252519f5/src/MahApps.Metro/Behaviors/WindowsSettingBehavior.cs#L128-L164","documentation":"WindowsSettingBehavior restores a previously saved window placement by calling WinApiHelper.SetWindowPlacement. If that native call throws (e.g. because the persisted RECT is invalid or came from a now-absent monitor), the behavior wraps the exception in a MahAppsException naming the window, preserving the original via InnerException. This protects app startup from a corrupt window-state setting.","triggerScenarios":"On window load, settings.Placement is non-empty so the behavior calls SetWindowPlacement(window, wp) and the underlying Win32 placement call throws — typically because the saved normalPosition is off-screen, degenerate, or incompatible with the current monitor topology.","commonSituations":"User previously closed the app on a monitor that is now disconnected (laptop dock change). RDP session with different resolution. Corrupted user settings file. DPI/position values that produce an invalid WINDOWPLACEMENT.","solutions":["Clear/reset the persisted window placement for the affected window (delete the user setting or set Placement to null) so the window opens with defaults.","Catch the MahAppsException at the app level and fall back to default bounds (CenterScreen) instead of crashing.","Update to a MahApps version that validates the saved RECT before calling SetWindowPlacement, or sanitize the normalPosition before restoring.","Reset the application settings (e.g. delete the user.config file) to discard the corrupt placement."],"exampleFix":"// before: behavior throws, app crashes on startup\n// after: guard in your window/settings code — clear bad placement before apply\nvar settings = window.GetWindowPlacementSettings();\nif (settings.Placement is { } p && !IsPlacementValid(p.normalPosition))\n{\n    settings.Placement = null; // discard corrupt placement\n}","handlingStrategy":"try-catch","validationCode":"// Sanitize the saved placement before letting the behavior apply it\nvar settings = window.GetWindowPlacementSettings();\nif (settings.Placement is { } p) {\n    var rect = p.normalPosition;\n    bool onScreen = SystemParameters.VirtualScreenLeft <= rect.Left\n                    && rect.Right <= SystemParameters.VirtualScreenLeft + SystemParameters.VirtualScreenWidth\n                    && (rect.Right - rect.Left) > 0;\n    if (!onScreen) settings.Placement = null; // discard, use defaults\n}","typeGuard":null,"tryCatchPattern":"try {\n    // open the window / let the behavior apply placement\n} catch (MahAppsException ex) when (ex.Message.Contains(\"Failed to set the window state\")) {\n    // reset persisted placement and reopen with defaults\n    window.GetWindowPlacementSettings().Placement = null;\n    Log.Warn(\"Discarded invalid window placement\", ex.InnerException);\n}","preventionTips":["Clamp saved window bounds to the virtual screen before persisting/restore.","Catch MahAppsException at the app level and fall back to CenterScreen for bad placements.","Reset user.config if windows repeatedly fail to open (corrupt settings).","Test multi-monitor scenarios (dock/undock, RDP) during development."],"tags":["wpf","window-state","interop","win32","settings","runtime"],"backgroundTag":null,"analyzedSha":"72099e310bac2d12ac98fd7560b69679252519f5","analyzedAt":"2026-08-13T20:19:34.419Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}