示例#1
0
        /// <summary>
        /// Gets the <see cref="Theme"/> with the given resource dictionary.
        /// </summary>
        /// <param name="resources"><see cref="ResourceDictionary"/> from which the theme should be retrieved.</param>
        /// <returns>The <see cref="Theme"/> or <c>null</c>, if the theme wasn't found.</returns>
        public static Theme GetTheme([NotNull] ResourceDictionary resources)
        {
            if (resources.IsNull())
            {
                throw new ArgumentNullException(nameof(resources));
            }

            var builtInTheme = Themes.FirstOrDefault(x => AreResourceDictionarySourcesEqual(x.Resources, resources));

            if (builtInTheme.IsNotNull())
            {
                return(builtInTheme);
            }

            // support dynamically created runtime resource dictionaries
            if (resources.Source.IsNull())
            {
                if (IsThemeDictionary(resources))
                {
                    return(new Theme(resources));
                }
            }

            return(null);
        }
示例#2
0
        private static bool AreResourceDictionarySourcesEqual(ResourceDictionary first, ResourceDictionary second)
        {
            if (first.IsNull() ||
                second.IsNull())
            {
                return(false);
            }

            if (first.Source.IsNull() ||
                second.Source.IsNull())
            {
                try
                {
                    foreach (var key in first.Keys)
                    {
                        var isTheSame = second.Contains(key) &&
                                        Equals(first[key], second[key]);
                        if (!isTheSame)
                        {
                            return(false);
                        }
                    }
                }
                catch (Exception exception)
                {
                    Trace.TraceError($"Could not compare resource dictionaries: {exception} {Environment.NewLine} {exception.StackTrace}");
                    return(false);
                }

                return(true);
            }

            return(Uri.Compare(first.Source, second.Source, UriComponents.Host | UriComponents.Path, UriFormat.SafeUnescaped, StringComparison.OrdinalIgnoreCase) == 0);
        }
示例#3
0
        public static Theme DetectTheme([NotNull] ResourceDictionary resourceDictionary)
        {
            if (resourceDictionary.IsNull())
            {
                throw new ArgumentNullException(nameof(resourceDictionary));
            }

            if (DetectThemeFromResources(out var currentTheme, resourceDictionary))
            {
                return(currentTheme);
            }

            return(null);
        }
示例#4
0
        public static void ApplyThemeResourcesFromTheme([NotNull] ResourceDictionary resources, [NotNull] Theme newTheme)
        {
            if (resources.IsNull())
            {
                throw new ArgumentNullException(nameof(resources));
            }

            if (newTheme.IsNull())
            {
                throw new ArgumentNullException(nameof(newTheme));
            }

            ApplyResourceDictionary(newTheme.Resources, resources);
        }
示例#5
0
        public static Theme ChangeTheme([NotNull] ResourceDictionary resourceDictionary, [NotNull] Theme newTheme)
        {
            if (resourceDictionary.IsNull())
            {
                throw new ArgumentNullException(nameof(resourceDictionary));
            }

            if (newTheme.IsNull())
            {
                throw new ArgumentNullException(nameof(newTheme));
            }

            var oldTheme = DetectTheme(resourceDictionary);

            return(ChangeTheme(resourceDictionary, oldTheme, newTheme));
        }
示例#6
0
        /// <summary>
        /// Determines whether the specified resource dictionary represents an <see cref="Theme"/>.
        /// <para />
        /// This might include runtime themes which do not have a resource uri.
        /// </summary>
        /// <param name="resources">The resources.</param>
        /// <returns><c>true</c> if the resource dictionary is an <see cref="Theme"/>; otherwise, <c>false</c>.</returns>
        /// <exception cref="System.ArgumentNullException">resources</exception>
        public static bool IsThemeDictionary([NotNull] ResourceDictionary resources)
        {
            if (resources.IsNull())
            {
                throw new ArgumentNullException(nameof(resources));
            }

            foreach (var styleKey in styleKeys)
            {
                // Note: do not use contains, because that will look in all merged dictionaries as well. We need to check
                // out the actual keys of the current resource dictionary
                if (!(from object resourceKey in resources.Keys
                      select resourceKey as string).Any(keyAsString => string.Equals(keyAsString, styleKey, StringComparison.Ordinal)))
                {
                    return(false);
                }
            }

            return(true);
        }
示例#7
0
        private static bool AreResourceDictionarySourcesEqual(ResourceDictionary first, ResourceDictionary second)
        {
            if (first.IsNull() ||
                second.IsNull())
            {
                return(false);
            }

            // If RD does not have a source, but both have keys and the first one has at least as many keys as the second,
            // then compares their values.
            if ((first.Source.IsNull() ||
                 second.Source.IsNull()) &&
                first.Keys.Count > 0 &&
                second.Keys.Count > 0 &&
                first.Keys.Count >= second.Keys.Count)
            {
                try
                {
                    foreach (var key in first.Keys)
                    {
                        var isTheSame = second.Contains(key) &&
                                        Equals(first[key], second[key]);
                        if (!isTheSame)
                        {
                            return(false);
                        }
                    }
                }
                catch (Exception exception)
                {
                    Trace.TraceError($"Could not compare resource dictionaries: {exception} {Environment.NewLine} {exception.StackTrace}");
                    return(false);
                }

                return(true);
            }

            return(Uri.Compare(first.Source, second.Source, UriComponents.Host | UriComponents.Path, UriFormat.SafeUnescaped, StringComparison.OrdinalIgnoreCase) == 0);
        }