Skip to content

fix: add JSON fallback for SaveWindowPosition on .NET 9#4567

Open
JyslaFancy wants to merge 5 commits into
MahApps:developfrom
JyslaFancy:fix/net9-save-window-position
Open

fix: add JSON fallback for SaveWindowPosition on .NET 9#4567
JyslaFancy wants to merge 5 commits into
MahApps:developfrom
JyslaFancy:fix/net9-save-window-position

Conversation

@JyslaFancy

Copy link
Copy Markdown

Description

Fixes #4541 — .NET 9 breaks SaveWindowPosition in MetroWindow.

Problem

On .NET 9 prior to version 9.0.3, ApplicationSettingsBase.Save() throws a
ConfigurationErrorsException when the project path is on a UNC/network location.
This is a known .NET runtime bug (dotnet/runtime#112410), fixed in .NET 9.0.3.

Fix

Adds a JSON-based fallback for window placement persistence:

  • Save: if settings.Save() fails → placement saved to
    %LocalAppData%//WindowPlacement/.json
  • Load: if settings.Reload() fails or returns empty → loads from JSON fallback
  • On success: JSON stays in sync

No new dependencies — uses System.Text.Json.

Harald Daltveit added 5 commits June 7, 2026 15:52
- ApplicationSettingsBase.Save() fails on .NET 9 < 9.0.3 due to a runtime
  bug (dotnet/runtime#112410) where ClientConfigurationHost receives an
  empty path on UNC/network locations.
- SaveWindowState() now saves a JSON backup to %LocalAppData% as a
  fallback when the settings save fails.
- LoadWindowState() tries the JSON fallback when settings reload fails
  or returns an empty placement.
- On successful save via ApplicationSettingsBase, the JSON backup is
  also updated to stay in sync.

Fixes MahApps#4541
System.Text.Json is not available on .NET Framework 4.6.2. Wrap JSON
fallback code with #if !NET462 to maintain compatibility with legacy
target framework.
Merge the two identical null/empty checks into one to eliminate
Codacy warning about redundant condition.
Capture settings.Placement in a local variable so the compiler can
track null-state through the #if blocks. Prevents CS8602 on
placement.ToWINDOWPLACEMENT().
Compiler nullable flow analysis can't track through #if blocks
and out-parameter narrowing. placement is guaranteed non-null
after the if-check (we return in all null branches).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

.NET 9 breaks SaveWindowPosition

1 participant