コード例 #1
0
        private static StyleItem GetStyleFromAssembly(FrameworkElement frameworkElement, Assembly themeAssembly, string bamlResourceName, string location)
        {
            var fieldInfo = typeof(FrameworkElement).GetField("DefaultStyleKeyProperty", BindingFlags.Static | BindingFlags.NonPublic);

            if (fieldInfo != null)
            {
                var    defaultStyleKeyProperty = (DependencyProperty)fieldInfo.GetValue(null);
                object defaultStyleKey         = frameworkElement.GetValue(defaultStyleKeyProperty);

                var names = themeAssembly.GetManifestResourceNames();
                foreach (var name in names)
                {
                    var stream = themeAssembly.GetManifestResourceStream(name);
                    foreach (Resource resource in new ResourceReader(stream))
                    {
                        if (resource.Name.Equals(bamlResourceName, StringComparison.InvariantCultureIgnoreCase))
                        {
                            var resourceDict = (ResourceDictionary)BamlLoader.LoadBaml((Stream)resource.Value);
                            if (resourceDict != null)
                            {
                                foreach (var key in resourceDict.Keys)
                                {
                                    if (key == defaultStyleKey && resourceDict[key] is Style)
                                    {
                                        return(new StyleItem((Style)resourceDict[key], frameworkElement, StyleHelper.GetKeyString(key), location, StyleScope.Theme));
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return(null);
        }
コード例 #2
0
        private static List <ResourceDictionary> GetResourcesFromThemeAssembly(Assembly assembly, string bamlResourceName)
        {
            var resourceDictionaries = new List <ResourceDictionary>();

            var names = assembly.GetManifestResourceNames();

            foreach (var name in names)
            {
                var stream = assembly.GetManifestResourceStream(name);
                foreach (Resource resource in new ResourceReader(stream))
                {
                    if (resource.Name.Equals(bamlResourceName, StringComparison.InvariantCultureIgnoreCase))
                    {
                        var resourceDictionary = BamlLoader.LoadBaml((Stream)resource.Value) as ResourceDictionary;
                        if (resourceDictionary != null)
                        {
                            resourceDictionaries.Add(resourceDictionary);
                        }
                    }
                }
            }
            return(resourceDictionaries);
        }