MahApps/MahApps.Metro · error · MahAppsException
Failed to set the window state for {window} from the setting
Error message
Failed to set the window state for {window} from the settings. What it means
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.
Source
Thrown at src/MahApps.Metro/Behaviors/WindowsSettingBehavior.cs:146
{
Trace.TraceError($"{this}: The settings for {window} could not be reloaded! {e}");
return;
}
// check for existing placement and prevent empty bounds
if (settings.Placement is null || settings.Placement.normalPosition.IsEmpty)
{
return;
}
try
{
var wp = settings.Placement.ToWINDOWPLACEMENT();
WinApiHelper.SetWindowPlacement(window, wp);
}
catch (Exception ex)
{
throw new MahAppsException($"Failed to set the window state for {window} from the settings.", ex);
}
}
private void SaveWindowState()
{
var window = this.AssociatedObject;
if (window is null)
{
return;
}
var settings = window.GetWindowPlacementSettings();
if (settings is null || !window.SaveWindowPosition)
{
return;
}
var windowHandle = new WindowInteropHelper(window).EnsureHandle();View on GitHub (pinned to 72099e310b)
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.
Example fix
// before: behavior throws, app crashes on startup
// after: guard in your window/settings code — clear bad placement before apply
var settings = window.GetWindowPlacementSettings();
if (settings.Placement is { } p && !IsPlacementValid(p.normalPosition))
{
settings.Placement = null; // discard corrupt placement
} Defensive patterns
Strategy: try-catch
Validate before calling
// Sanitize the saved placement before letting the behavior apply it
var settings = window.GetWindowPlacementSettings();
if (settings.Placement is { } p) {
var rect = p.normalPosition;
bool onScreen = SystemParameters.VirtualScreenLeft <= rect.Left
&& rect.Right <= SystemParameters.VirtualScreenLeft + SystemParameters.VirtualScreenWidth
&& (rect.Right - rect.Left) > 0;
if (!onScreen) settings.Placement = null; // discard, use defaults
} Try / catch
try {
// open the window / let the behavior apply placement
} catch (MahAppsException ex) when (ex.Message.Contains("Failed to set the window state")) {
// reset persisted placement and reopen with defaults
window.GetWindowPlacementSettings().Placement = null;
Log.Warn("Discarded invalid window placement", ex.InnerException);
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Could not locate any instances of contract {contract}.
- Could not locate any instances of contract {typeof(T)}.
- Uups, it seems like there is something wrong with the given
- Cannot convert the given input to a valid color
- The inverse dialog theme only works if the window theme abid
AI-assisted analysis of MahApps/MahApps.Metro@72099e310b (2026-08-13).
Data as JSON: /api/errors/a268b2ef931c1b4c.
Report an issue: GitHub.