Click here to Skip to main content
15,881,881 members
Articles / Desktop Programming / WPF

Pluggable Styles and Resources in WPF with Language Converter Tool

Rate me:
Please Sign up or sign in to vote.
4.96/5 (17 votes)
31 Oct 2010CPOL9 min read 41K   748   31  
In this article, I have shown how you can build pluggable Resources for styles, Languages or any static objects, etc. Therefore building a new style doesn't hamper your code and you can easily plugin any new style to the application even though it is already in the production environment.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Collections.ObjectModel;
using System.Configuration;
using System.Reflection;
using System.IO;
using ResourceBase;
using System.Windows.Media;
using System.Globalization;
using System.Threading;

namespace MainWpfApplication
{
    public static class ResourceUtil
    {
        public static Dictionary<IResourceAttribute, Assembly> AvailableStyles = new Dictionary<IResourceAttribute, Assembly>();
        public static Dictionary<IResourceAttribute, Assembly> AvailableDictionaries = new Dictionary<IResourceAttribute, Assembly>();
        public static Dictionary<IResourceAttribute, Assembly> AvailableTypes = new Dictionary<IResourceAttribute, Assembly>();

        public static Color BaseColor { get; set; }
        public static ResourceDictionary GetAppropriateDictionary()
        {
           //Get Styles Folder path
           string path = ConfigurationSettings.AppSettings["stylefolderpath"];
           string currentTheme = ConfigurationSettings.AppSettings["CurrentTheme"];
           ResourceUtil.LoadAssemblies(AvailableStyles, path);
           IResourceAttribute currentResource = AvailableStyles.Keys.FirstOrDefault<IResourceAttribute>(item => item.Name.Equals(currentTheme));
           StyleResourceAttribute sra = currentResource as StyleResourceAttribute;
           if(sra != null)
                BaseColor = sra.BaseColor;// We can do this as we are fetching from AvailableStyles.
           if (currentResource != null)
           {
               Assembly currentAssembly = AvailableStyles[currentResource];
               Type resourceType = currentAssembly.GetType(currentResource.ResourceClassPath);
               ConstructorInfo cinfo = resourceType.GetConstructor(Type.EmptyTypes);
               ResourceDictionary dictionary = cinfo.Invoke(new object[] { }) as ResourceDictionary;
               return dictionary;
           }
           return null;
        }
        public static ResourceDictionary GetAppropriateLanguage()
        {
            //Get Language Folder path
            string path = ConfigurationSettings.AppSettings["languagefolderpath"];
            CultureInfo currentCulture = Thread.CurrentThread.CurrentCulture;
            ResourceUtil.LoadAssemblies(AvailableDictionaries, path);
            IResourceAttribute currentResource = AvailableDictionaries.Keys.FirstOrDefault<IResourceAttribute>(item => 
                {
                    LocalizationResourceAttribute la = item as LocalizationResourceAttribute;
                    if (la != null)
                        return la.Culture.Equals(currentCulture);
                    return false;
                });

            if (currentResource != null)
            {
                Assembly currentAssembly = AvailableDictionaries[currentResource];
                Type resourceType = currentAssembly.GetType(currentResource.ResourceClassPath);
                ConstructorInfo cinfo = resourceType.GetConstructor(Type.EmptyTypes);
                ResourceDictionary dictionary = cinfo.Invoke(new object[] { }) as ResourceDictionary;
                return dictionary;
            }
            return null;

        }
        public static ResourceDictionary LoadObjects()
        {
            //Get Object Folder path
            string path = ConfigurationSettings.AppSettings["objectsfolderpath"];
            ResourceUtil.LoadAssemblies(AvailableTypes, path);
            IResourceAttribute currentResource = AvailableTypes.Keys.FirstOrDefault<IResourceAttribute>();
            
            if (currentResource != null)
            {
                Assembly currentAssembly = AvailableStyles[currentResource];
                Type resourceType = currentAssembly.GetType(currentResource.ResourceClassPath);
                ConstructorInfo cinfo = resourceType.GetConstructor(Type.EmptyTypes);
                ResourceDictionary dictionary = cinfo.Invoke(new object[] { }) as ResourceDictionary;
                return dictionary;
            }
            return null;
        }

        private static void LoadAssemblies(Dictionary<IResourceAttribute, Assembly> resource, string path)
        {
            DirectoryInfo di = new DirectoryInfo(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), path));
            foreach (FileInfo fi in di.GetFiles())
            {
                try
                {
                    Assembly thisAssembly = Assembly.LoadFile(fi.FullName);
                    var attributes = thisAssembly.GetCustomAttributes(true);
                    IEnumerable<object> resourceAttributes = attributes.Where(item => item is IResourceAttribute);
                    foreach (IResourceAttribute raatr in resourceAttributes)
                        resource.Add(raatr, thisAssembly);
                }
                catch { }
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
President
India India
Did you like his post?

Oh, lets go a bit further to know him better.
Visit his Website : www.abhisheksur.com to know more about Abhishek.

Abhishek also authored a book on .NET 4.5 Features and recommends you to read it, you will learn a lot from it.
http://bit.ly/EXPERTCookBook

Basically he is from India, who loves to explore the .NET world. He loves to code and in his leisure you always find him talking about technical stuffs.

Working as a VP product of APPSeCONNECT, an integration platform of future, he does all sort of innovation around the product.

Have any problem? Write to him in his Forum.

You can also mail him directly to abhi2434@yahoo.com

Want a Coder like him for your project?
Drop him a mail to contact@abhisheksur.com

Visit His Blog

Dotnet Tricks and Tips



Dont forget to vote or share your comments about his Writing

Comments and Discussions