|
3 | 3 | using System.Linq;
|
4 | 4 | using System.Text;
|
5 | 5 | using System.Threading.Tasks;
|
| 6 | +using System.Text.RegularExpressions; |
| 7 | +using System.Windows; |
6 | 8 |
|
7 | 9 | namespace MaterialDesignColors.WpfExample
|
8 | 10 | {
|
9 | 11 | public class PaletteSelectorViewModel
|
10 | 12 | {
|
11 |
| - } |
| 13 | + public PaletteSelectorViewModel() |
| 14 | + { |
| 15 | + AscertainCurrentResourceDictionaries(); |
| 16 | + } |
| 17 | + |
| 18 | + private void AscertainCurrentResourceDictionaries() |
| 19 | + { |
| 20 | + var lightDarkResourceDictionary = Application.Current.Resources.MergedDictionaries |
| 21 | + .Where(rd => rd.Source != null) |
| 22 | + .SingleOrDefault(rd => Regex.Match(rd.Source.AbsolutePath, @"(\/MaterialDesignThemes.Wpf;component\/Themes\/MaterialDesignTheme\.)((Light)|(Dark))").Success); |
| 23 | + if (lightDarkResourceDictionary == null) |
| 24 | + throw new ApplicationException("Unable to find Light/Dark base theme in Application resources."); |
| 25 | + var lightDarkDescription = Regex.Match(lightDarkResourceDictionary.Source.AbsolutePath, "(?'desc'(Light)|(Dark))(.xaml)$").Groups[ |
| 26 | + "desc"].Value; |
| 27 | + |
| 28 | + ResourceDictionary primaryColorResourceDictionary; |
| 29 | + if (!TryFindSwatchDictionary(Application.Current.Resources, "PrimaryHueMidBrush", out primaryColorResourceDictionary)) |
| 30 | + throw new ApplicationException("Unable to find primary color definition in Application resources."); |
| 31 | + |
| 32 | + ResourceDictionary accentolorResourceDictionary; |
| 33 | + if (!TryFindSwatchDictionary(Application.Current.Resources, "SecondaryAccentBrush", out accentolorResourceDictionary)) |
| 34 | + throw new ApplicationException("Unable to find accent color definition in Application resources."); |
| 35 | + } |
| 36 | + |
| 37 | + private bool TryFindSwatchDictionary(ResourceDictionary parentDictionary, string expectedBrushName, out ResourceDictionary dictionary) |
| 38 | + { |
| 39 | + dictionary = parentDictionary.MergedDictionaries.SingleOrDefault(rd => rd[expectedBrushName] != null); |
| 40 | + return dictionary != null; |
| 41 | + } |
| 42 | + } |
12 | 43 | }
|
0 commit comments