private static void ApplyResourceDictionary(ResourceDictionary newRd, ResourceDictionary oldRd) { foreach (DictionaryEntry r in newRd) { if (oldRd.Contains(r.Key)) oldRd.Remove(r.Key); oldRd.Add(r.Key, r.Value); } }
private static void ChangeTheme(ResourceDictionary resources, Tuple<Theme, Accent> oldThemeInfo, Accent accent, Theme newTheme) { if (oldThemeInfo != null) { var oldAccent = oldThemeInfo.Item2; if (oldAccent != null && oldAccent.Name != accent.Name) { var accentResource = resources.MergedDictionaries.FirstOrDefault(d => d.Source == oldAccent.Resources.Source); if (accentResource != null) { var ok = resources.MergedDictionaries.Remove(accentResource); // really need this??? foreach (DictionaryEntry r in accentResource) { if (resources.Contains(r.Key)) resources.Remove(r.Key); } resources.MergedDictionaries.Add(accent.Resources); } } var oldTheme = oldThemeInfo.Item1; if (oldTheme != null && oldTheme != newTheme) { var oldThemeResource = (oldTheme == Theme.Light) ? LightResource : DarkResource; var md = resources.MergedDictionaries.FirstOrDefault(d => d.Source == oldThemeResource.Source); if (md != null) { var ok = resources.MergedDictionaries.Remove(md); var newThemeResource = (newTheme == Theme.Light) ? LightResource : DarkResource; // really need this??? foreach (DictionaryEntry r in oldThemeResource) { if (resources.Contains(r.Key)) resources.Remove(r.Key); } resources.MergedDictionaries.Add(newThemeResource); } } } else { ChangeTheme(resources, accent, newTheme); } }
private void SetThemeSource(Uri source, bool useThemeAccentColor) { if (source == null) { throw new ArgumentNullException("source"); } var oldThemeDict = GetThemeDictionary(); var dictionaries = Application.Current.Resources.MergedDictionaries; var themeDict = new ResourceDictionary { Source = source }; // if theme defines an accent color, use it var accentColor = themeDict[KeyAccentColor] as Color?; if (accentColor.HasValue) { // remove from the theme dictionary and apply globally if useThemeAccentColor is true themeDict.Remove(KeyAccentColor); if (useThemeAccentColor) { ApplyAccentColor(accentColor.Value); } } // add new before removing old theme to avoid dynamicresource not found warnings dictionaries.Add(themeDict); // remove old theme if (oldThemeDict != null) { dictionaries.Remove(oldThemeDict); } OnPropertyChanged("ThemeSource"); }
public void MergedDictionaries_KeyClashes () { var key = "Key"; var value = new object (); var value1 = new object (); var value2 = new object (); var merge1 = new ResourceDictionary (); var merge2 = new ResourceDictionary (); ResourceDictionary main = new ResourceDictionary (); main.MergedDictionaries.Add (merge1); main.MergedDictionaries.Add (merge2); main.Add (key, value); merge1.Add (key, value1); merge2.Add (key, value2); // We ignore the values in the merged dictionaries if the main dictionary // has the key Assert.AreEqual (value, main [key], "#1"); main.Remove (key); // We should look up the merged dictionaries in reverse order Assert.AreEqual (value2,main [key], "#2"); merge2.Remove (key); // Now we should find it in the first merged dictionary Assert.AreEqual (value1, main [key], "#3"); }
public void Contains_TypeAndStringInResourceDictionary_TypeFirst() { var rd = new ResourceDictionary(); rd.Add(typeof(Button), new Style { TargetType = typeof(Button) }); rd.Add("System.Windows.Controls.Button", "System.Windows.Controls.Button"); Assert.IsTrue(rd.Contains(typeof(Button)), "#1"); Assert.IsTrue(rd.Contains("System.Windows.Controls.Button"), "#2"); rd.Remove(typeof(Button)); Assert.IsFalse(rd.Contains(typeof(Button)), "#3"); Assert.IsTrue(rd.Contains("System.Windows.Controls.Button"), "#4"); }
private void LoadResources(string[] uriStrings) { foreach (string uri in uriStrings) { ResourceDictionary resources = new ResourceDictionary(); resources.Source = new Uri(uri, UriKind.Relative); foreach (object key in resources.Keys) { object value = resources[key]; resources.Remove(key); this.Resources.Add(key, value); } } }
static void SetThemeSource(Uri source) { var oldThemeDict = GetThemeDictionary(); var dictionaries = Application.Current.Resources.MergedDictionaries; var themeDict = new ResourceDictionary { Source = source }; // if theme defines an accent color, use it var accentColor = themeDict[KeyAccentColor] as Color?; if (accentColor.HasValue) // remove from the theme dictionary and apply globally if useThemeAccentColor is true themeDict.Remove(KeyAccentColor); // add new before removing old theme to avoid dynamicresource not found warnings dictionaries.Add(themeDict); // remove old theme if (oldThemeDict != null) dictionaries.Remove(oldThemeDict); }