@@ -43,7 +43,6 @@ internal static void InitializeFluentTheme()
4343 var themeColorResourceUri = GetFluentWindowThemeColorResourceUri ( _currentApplicationTheme , _currentUseLightMode ) ;
4444 Application . Current . Resources . MergedDictionaries . Add ( new ResourceDictionary ( ) { Source = themeColorResourceUri } ) ;
4545
46- DwmColorization . UpdateAccentColors ( ) ;
4746 _isFluentThemeInitialized = true ;
4847 }
4948 }
@@ -78,7 +77,7 @@ internal static void ApplySystemTheme(IEnumerable windows = null, bool forceUpda
7877
7978 string systemTheme = GetSystemTheme ( ) ;
8079 bool useLightMode = IsSystemThemeLight ( ) ;
81- Color systemAccentColor = DwmColorization . GetSystemAccentColor ( ) ;
80+ Color systemAccentColor = AccentColorHelper . SystemAccentColor ;
8281 ApplyTheme ( windows , systemTheme , useLightMode , systemAccentColor , forceUpdate ) ;
8382 }
8483
@@ -101,9 +100,8 @@ private static void ApplyTheme(
101100 if ( forceUpdate ||
102101 requestedTheme != _currentApplicationTheme ||
103102 requestedUseLightMode != _currentUseLightMode ||
104- DwmColorization . GetSystemAccentColor ( ) != DwmColorization . CurrentApplicationAccentColor )
103+ requestedAccentColor != _currentSystemAccentColor )
105104 {
106- DwmColorization . UpdateAccentColors ( ) ;
107105
108106 Uri dictionaryUri = GetFluentWindowThemeColorResourceUri ( requestedTheme , requestedUseLightMode ) ;
109107 AddOrUpdateThemeResources ( dictionaryUri ) ;
@@ -121,6 +119,7 @@ private static void ApplyTheme(
121119
122120 _currentApplicationTheme = requestedTheme ;
123121 _currentUseLightMode = requestedUseLightMode ;
122+ _currentSystemAccentColor = requestedAccentColor ;
124123 }
125124 }
126125
@@ -192,18 +191,14 @@ private static void AddOrUpdateThemeResources(Uri dictionaryUri)
192191
193192 var newDictionary = new ResourceDictionary ( ) { Source = dictionaryUri } ;
194193
195- ResourceDictionary currentDictionary = Application . Current ? . Resources ;
196- foreach ( var key in newDictionary . Keys )
194+ FindFluentThemeAndColorDictionary ( out ResourceDictionary fluentDictionary , out ResourceDictionary colorDictionary ) ;
195+
196+ if ( colorDictionary != null )
197197 {
198- if ( currentDictionary . Contains ( key ) )
199- {
200- currentDictionary [ key ] = newDictionary [ key ] ;
201- }
202- else
203- {
204- currentDictionary . Add ( key , newDictionary [ key ] ) ;
205- }
198+ Application . Current . Resources . MergedDictionaries . Remove ( colorDictionary ) ;
206199 }
200+
201+ Application . Current . Resources . MergedDictionaries . Add ( newDictionary ) ;
207202 }
208203
209204 #endregion
@@ -232,6 +227,31 @@ private static Uri GetFluentWindowThemeColorResourceUri(string systemTheme, bool
232227 return new Uri ( "pack://application:,,,/PresentationFramework.Fluent;component/Resources/Theme/" + themeColorFileName , UriKind . Absolute ) ;
233228 }
234229
230+ private static void FindFluentThemeAndColorDictionary ( out ResourceDictionary fluentThemeDictionary , out ResourceDictionary fluentColorDictionary )
231+ {
232+ fluentThemeDictionary = null ;
233+ fluentColorDictionary = null ;
234+
235+ if ( Application . Current == null ) return ;
236+
237+ foreach ( ResourceDictionary mergedDictionary in Application . Current . Resources . MergedDictionaries )
238+ {
239+ if ( mergedDictionary . Source != null )
240+ {
241+ if ( mergedDictionary . Source . ToString ( ) == fluentResourceDictionaryUri )
242+ {
243+ fluentThemeDictionary = mergedDictionary ;
244+ }
245+ else if ( mergedDictionary . Source . ToString ( ) . StartsWith ( fluentColorResourceUriPart ) )
246+ {
247+ fluentColorDictionary = mergedDictionary ;
248+ }
249+ }
250+ }
251+ }
252+
253+
254+
235255 #endregion
236256
237257 #region Private Members
@@ -240,6 +260,9 @@ private static Uri GetFluentWindowThemeColorResourceUri(string systemTheme, bool
240260
241261 private static readonly string _regPersonalizeKeyPath = "HKEY_CURRENT_USER\\ Software\\ Microsoft\\ Windows\\ CurrentVersion\\ Themes\\ Personalize" ;
242262
263+ private static readonly string fluentResourceDictionaryUri = "pack://application:,,,/PresentationFramework.Fluent;component/Resources/Fluent.xaml" ;
264+ private static readonly string fluentColorResourceUriPart = "pack://application:,,,/PresentationFramework.Fluent;component/Resources/Theme/" ;
265+
243266 private static string _currentApplicationTheme ;
244267
245268 private static bool _currentUseLightMode = true ;
@@ -248,5 +271,7 @@ private static Uri GetFluentWindowThemeColorResourceUri(string systemTheme, bool
248271
249272 private static bool _isFluentThemeInitialized = false ;
250273
274+ private static Color _currentSystemAccentColor = AccentColorHelper . SystemAccentColor ;
275+
251276 #endregion
252277}
0 commit comments