fix: add JSON fallback for SaveWindowPosition on .NET 9#4567
Open
JyslaFancy wants to merge 5 commits into
Open
fix: add JSON fallback for SaveWindowPosition on .NET 9#4567JyslaFancy wants to merge 5 commits into
JyslaFancy wants to merge 5 commits into
Conversation
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).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
%LocalAppData%//WindowPlacement/.json
No new dependencies — uses System.Text.Json.