This is just a rough draft for a POC!
The Dictionary hierarchy is;
Colors
Brushes
Styles
So the ordering is important
I'm trying to change a theme in WPF, I can do it like this;
+ private void ChangeTheme(string themeName)
+ {
+ ResourceDictionary resource = null;
+ using (FileStream fs = new FileStream("../../../Resources/ResourceDictionaries/" + themeName + ".xaml", FileMode.Open, FileAccess.Read))
+ resource = (ResourceDictionary)XamlReader.Load(fs);
+
+ Application.Current.Resources.MergedDictionaries.Clear();
+ Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary()
+ { Source = new Uri("pack://application:,,,/Resources/ResourceDictionaries/dictionary1.xaml") });
+ Application.Current.Resources.MergedDictionaries.Add(resource);
+ Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary()
+ { Source = new Uri("pack://application:,,,/Resources/ResourceDictionaries/Monotone.Brushes.xaml") });
+ Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary()
+ { Source = new Uri("pack://application:,,,/Resources/ResourceDictionaries/Monotone.xaml") });
+ }
But if I add more ResourceDictionaries to the project I would have to come back here and modify to the list.
What I have tried:
So I thought I would do it this way but it doesn't seem to work. Theme doesn't change! The new theme gets loaded, the old dictionaries get transferred to dictionaryCollection and everything gets added back to Application.Current.Resources.MergedDictionariy but the new theme is not shown?
I've googled for a couple of hours and everything I've found says this should work?
private void ChangeTheme(string themeName)
{
ResourceDictionary resource = null;
string file = "../../../Resources/ResourceDictionaries/" + themeName + ".xaml";
//using (FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read))
// resource = (ResourceDictionary)XamlReader.Load(fs);
resource = new ResourceDictionary();
resource.Source = new Uri(file, UriKind.Relative);
ResourceDictionary rd = GetActualResourceDictionary();
if (rd != null)
Application.Current.Resources.MergedDictionaries.Remove(rd);
// Create collection to hold old RecourceDictionaries
System.Collections.ObjectModel.Collection<ResourceDictionary> dictionaryCollection = new
System.Collections.ObjectModel.Collection<ResourceDictionary>();
// Copy REsourceDictionaries left after removing the Theme
foreach (ResourceDictionary dict in Application.Current.Resources.MergedDictionaries)
dictionaryCollection.Add(dict);
Application.Current.Resources.MergedDictionaries.Clear();
Application.Current.Resources.MergedDictionaries.Add(resource);
foreach (ResourceDictionary dict in dictionaryCollection)
Application.Current.Resources.MergedDictionaries.Add(dict);
}
//NOTE This works but the
static ResourceDictionary GetActualResourceDictionary()
{
// get the actual ResourceDictionary
foreach (ResourceDictionary res in Application.Current.Resources.MergedDictionaries)
{
if (res.Source != null)
{
string source = res.Source.ToString();
if (source.ToUpper().Contains("THEME"))
return res;
}
}
return null;
}