|
1 | 1 | namespace Trimble.Modus.Components.Helpers; |
2 | | - |
3 | | -public static class ResourcesDictionary |
4 | | -{ |
5 | | - public static Color GetColor(string styleKey) |
6 | | - { |
7 | | - if (Application.Current.Resources.ContainsKey(styleKey)) |
8 | | - { |
9 | | - return Application.Current.Resources[styleKey] as Color; |
10 | | - } |
11 | | - else |
12 | | - { |
13 | | - ResourceDictionary style = new Styles.LightThemeColors(); |
14 | | - if (Application.Current.RequestedTheme == AppTheme.Dark) |
15 | | - { |
16 | | - style = new Styles.DarkThemeColors(); |
17 | | - } |
18 | | - if (style.ContainsKey(styleKey)) |
19 | | - { |
20 | | - return style[styleKey] as Color; |
21 | | - } |
22 | | - } |
23 | | - return Colors.Transparent; |
24 | | - } |
25 | | -} |
| 2 | + |
| 3 | +public static class ResourcesDictionary |
| 4 | +{ |
| 5 | + public static Color GetColor(string styleKey) |
| 6 | + { |
| 7 | + if (Application.Current?.Resources is ResourceDictionary resources && |
| 8 | + resources.TryGetValue(styleKey, out var applicationResource) && |
| 9 | + applicationResource is Color applicationColor) |
| 10 | + { |
| 11 | + return applicationColor; |
| 12 | + } |
| 13 | + |
| 14 | + if (Application.Current?.Resources is ResourceDictionary appResources) |
| 15 | + { |
| 16 | + var preferredType = Application.Current.RequestedTheme == AppTheme.Dark |
| 17 | + ? typeof(Styles.DarkThemeColors) |
| 18 | + : typeof(Styles.LightThemeColors); |
| 19 | + |
| 20 | + foreach (var dictionary in appResources.MergedDictionaries) |
| 21 | + { |
| 22 | + if (dictionary.GetType() == preferredType && |
| 23 | + dictionary.TryGetValue(styleKey, out var themeResource) && |
| 24 | + themeResource is Color themeColor) |
| 25 | + { |
| 26 | + return themeColor; |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + foreach (var dictionary in appResources.MergedDictionaries) |
| 31 | + { |
| 32 | + if (dictionary.TryGetValue(styleKey, out var mergedResource) && |
| 33 | + mergedResource is Color mergedColor) |
| 34 | + { |
| 35 | + return mergedColor; |
| 36 | + } |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + return Colors.Transparent; |
| 41 | + } |
| 42 | +} |
0 commit comments