WPF + Theme management

Theme management

 

1.      Create a structure like the next one in the Resource folder of your solution.

<Resources>

                <Themes>

                               <ThemeBlue>

                                               Use here the xaml you need

                               <ThemeRed>

                                               Use here the xaml you need

                               ThemeBlue.xml

                               ThemeRed.xml

 

2.      Fill the ThemeBlue.xml by registering the xaml of the corresponding folder:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"&gt;

       <ResourceDictionary.MergedDictionaries>

             <ResourceDictionary                      Source="/RibbonControlsLibrary;component/Themes/Office2007Black.xaml" />

             <ResourceDictionary Source="Black\ExpressionDark.xaml" />

             <ResourceDictionary Source="Black\Windows.xaml" />

       </ResourceDictionary.MergedDictionaries>

</ResourceDictionary>

 

3.      Be sure that xaml properties are Page + Do not copy + MSBuild:Compile, so that xaml will be integrated in the final assembly.

 

4.      Load the theme with a function like this, just by passing the required color as a string:

static public void LoadTheme(string themeColor)

{

       Application.Current.Resources =

        (ResourceDictionary)Application.LoadComponent(

                    new Uri(string.Format("Resources\\Themes\\{0}.xaml",

                                                      themeColor), UriKind.Relative));

}

 This works also for files. So you can choose to externalize the xaml, discover by code the files and folder in the <Theme> subdirectory of the application running path

I have updated the code, so that I discover the theme embedded in the resources, and then fill the menu item. No more hard coded

 

check http://nperf.codeplex.com/

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s