示例#1
0
        /// <summary>
        /// Applys the theme by modifying App Resources.
        /// </summary>
        /// <remarks>This method is called heavily by the theme editor.</remarks>
        /// <param name="theme">The theme.</param>
        public void ApplyTheme(Theme theme)
        {
            // convert theme to ResourceDictionary
            var themeResourceDictionary = theme.ToResourceDictionary();

            var mergedDictionaries = Application.Current.Resources.MergedDictionaries;
            // need to remove the last theme resource dictionary
            if (ActiveTheme != null) {
                mergedDictionaries.RemoveAt(mergedDictionaries.Count - 1);
            }
            Application.Current.Resources.MergedDictionaries.Add(themeResourceDictionary);

            ActiveTheme = theme;
        }
示例#2
0
        /// <summary>
        /// Initializes the default (fallback) theme, which is used when no theme is loaded.
        /// Also if a theme doesn't specify a color, the color from this theme is used.
        /// </summary>
        public void InitializeDefaultTheme()
        {
            // load the default theme json from Resources
            var path = new Uri("Themes/default.json", UriKind.Relative);
            var resource = Application.GetResourceStream(path);
            var reader = new StreamReader(resource.Stream);
            var json = reader.ReadToEnd();

            // parse the json and load into a Theme object
            dynamic result = JsonConvert.DeserializeObject(json);
            Dictionary<string, string> config = result.ToObject<Dictionary<string, string>>();
            DefaultTheme = _themeFromConfig(config);

            // insert the resource dictionary to application resources
            var mergedDictionaries = Application.Current.Resources.MergedDictionaries;
            mergedDictionaries[1].MergedDictionaries[0] = DefaultTheme.ToResourceDictionary();
        }
示例#3
0
        public void TestResourceDictionary()
        {
            var theme = new Theme();
            theme.LoadFromPath(_themePath);

            var resourceDictionary = theme.ToResourceDictionary();
            // test the resource dictionary for correctness by comparing each color
            foreach (var colorKey in _colorKeys) {
                var actualColor = ((SolidColorBrush) resourceDictionary[colorKey]).Color;
                var expectedColor =
                    ((SolidColorBrush) new BrushConverter().ConvertFromString(_themeDict[colorKey])).Color;
                Assert.That(actualColor, Is.EqualTo(expectedColor));
            }
        }