Fix: Silently handle InvalidCastException from WPF system resource invalidation on Windows theme change

Co-authored-by: Jack251970 <53996452+Jack251970@users.noreply.github.com>
This commit is contained in:
copilot-swe-agent[bot] 2026-02-27 14:45:56 +00:00
parent 1146d53792
commit 372f142b49
2 changed files with 25 additions and 0 deletions

View file

@ -25,6 +25,12 @@ public static class ErrorReporting
// This change modifies the behavior to log the exception instead of showing the "Error report UI". // This change modifies the behavior to log the exception instead of showing the "Error report UI".
if (ExceptionHelper.IsRecoverableDwmCompositionException(e)) return; if (ExceptionHelper.IsRecoverableDwmCompositionException(e)) return;
// Workaround for a WPF issue where changing the Windows theme or accent color triggers
// SystemResources.InvalidateTreeResources, which tries to clone Color values stored in styles
// and fails with an InvalidCastException. This is a benign framework-level exception that
// does not affect Flow Launcher functionality, so we log it silently instead of showing the error dialog.
if (ExceptionHelper.IsRecoverableSystemResourceException(e)) return;
var reportWindow = new ReportWindow(e); var reportWindow = new ReportWindow(e);
reportWindow.Show(); reportWindow.Show();
} }

View file

@ -39,4 +39,23 @@ internal static class ExceptionHelper
return !string.IsNullOrEmpty(stackTrace) && return !string.IsNullOrEmpty(stackTrace) &&
stackTrace.Contains("DwmCompositionChanged", StringComparison.OrdinalIgnoreCase); stackTrace.Contains("DwmCompositionChanged", StringComparison.OrdinalIgnoreCase);
} }
/// <summary>
/// Returns true if the exception is a recoverable WPF system resource invalidation exception
/// that occurs when Windows changes its theme or accent colors. This is a known WPF issue where
/// <c>Color</c> values stored in styles are incorrectly cloned during resource tree invalidation.
/// </summary>
internal static bool IsRecoverableSystemResourceException(Exception exception)
{
if (exception is not InvalidCastException)
{
return false;
}
// Check for the specific Color-to-Expression cast failure originating from WPF's
// SystemResources.InvalidateTreeResources, triggered by Windows theme/accent color changes.
var stackTrace = exception.StackTrace;
return !string.IsNullOrEmpty(stackTrace) &&
stackTrace.Contains("System.Windows.SystemResources.InvalidateTreeResources", StringComparison.Ordinal);
}
} }