From a83da2b00f8d833b7431c308c0b4425ca9dfb355 Mon Sep 17 00:00:00 2001 From: Jack251970 <1160210343@qq.com> Date: Sat, 28 Feb 2026 23:08:53 +0800 Subject: [PATCH] Improve Theme resource handling and suppress async warnings Add try-catch to UpdateResourceDictionary to prevent crashes from InvalidCastException when updating resources. Set _oldResource to null on error. Suppress VSTHRD103 warnings around SetBlurForWindow to clarify intentional async usage and avoid build warnings. --- Flow.Launcher.Core/Resource/Theme.cs | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/Flow.Launcher.Core/Resource/Theme.cs b/Flow.Launcher.Core/Resource/Theme.cs index b657a5950..76a74b711 100644 --- a/Flow.Launcher.Core/Resource/Theme.cs +++ b/Flow.Launcher.Core/Resource/Theme.cs @@ -99,12 +99,6 @@ namespace Flow.Launcher.Core.Resource private void UpdateResourceDictionary(ResourceDictionary dictionaryToUpdate) { - // Add new resources - if (!Application.Current.Resources.MergedDictionaries.Contains(dictionaryToUpdate)) - { - Application.Current.Resources.MergedDictionaries.Add(dictionaryToUpdate); - } - // Remove old resources if (_oldResource != null && _oldResource != dictionaryToUpdate && Application.Current.Resources.MergedDictionaries.Contains(_oldResource)) @@ -112,7 +106,20 @@ namespace Flow.Launcher.Core.Resource Application.Current.Resources.MergedDictionaries.Remove(_oldResource); } - _oldResource = dictionaryToUpdate; + // Add new resources + try + { + if (!Application.Current.Resources.MergedDictionaries.Contains(dictionaryToUpdate)) + { + Application.Current.Resources.MergedDictionaries.Add(dictionaryToUpdate); + } + _oldResource = dictionaryToUpdate; + } + catch (InvalidCastException) + { + // System.InvalidCastException: Unable to cast object of type 'System.Windows.Media.Color' to type 'System.Windows.Expression'. + _oldResource = null; + } } /// @@ -654,7 +661,9 @@ namespace Flow.Launcher.Core.Resource { AutoDropShadow(useDropShadowEffect); } +#pragma warning disable VSTHRD103 // Call async methods when in an async method SetBlurForWindow(_settings.Theme, backdropType); +#pragma warning restore VSTHRD103 // Call async methods when in an async method if (!BlurEnabled) {