views:

741

answers:

2

Whenever I create a new WPF application or WPF user control library, the AssemblyInfo.cs file includes the following attribute:

[assembly: ThemeInfo(
    ResourceDictionaryLocation.None, 
    //where theme specific resource dictionaries are located
    //(used if a resource is not found in the page, 
    // or application resource dictionaries)
    ResourceDictionaryLocation.SourceAssembly 
    //where the generic resource dictionary is located
    //(used if a resource is not found in the page, 
    // app, or any theme specific resource dictionaries)
)]

What is this ThemeInfo attribute for? Will I break anything if I remove it?

+1  A: 

ThemeInfo attribute specifies where the automatic theming mechanism should look for the theme dictionaries and the generic dictionary. Each option can be set to one of the following values:

  • None (default): Don’t look for a resource dictionary.
  • SourceAssembly: The dictionary is the current assembly.
  • ExternalAssembly: The dictionary is in a different assembly, which must be named <AssemblyName>.<ThemeName>.dll, where <AssemblyName> is the current assembly's name.

If the theme dictionaries specify styles for controls that are defined in external assemblies, for example, the WPF controls such as System.Windows.Controls.ProgressBar and System.Windows.Button, then you must use the ThemeDictionaryExtension to specify the application as the source for the theme dictionaries.

ArsenMkrt
But what does that mean? What is a "theme dictionary"? What is a "generic dictionary"? What are they for? Are they important? And, like I asked in my question, will I break anything if I remove the attribute?
Joe White
As I know if you will remove that your controls will not take theme from generic.xaml, it says where to search generic theme
ArsenMkrt
A: 

The WPF framework uses this attribute in control libraries as a convenient way to apply resources to controls.

Consider that Windows can be run with different UI themes (Aero is one such example). The WPF controls provided by Microsoft alter their appearance for different environment themes.

If your application requires this behaviour, then you can create different theme dictionaries the in the themes folder of your control library project.

Even if you don't need multi-theme support, it is convenient to put resources in the generic.xaml file so that they are accessible to controls in the assembly. Perhaps your element (control) is defined in a .cs file without a .xaml partial class, and you need somewhere to store the resources it needs, or (more likely) you have resources that will be shared between many WPF elements in the same project/assembly.

The attribute you're referring to here is metadata for the mapping of these resources.

Drew Noakes
So I don't need the attribute if I'm using standard themable controls like Button? I would only need it if: (a) I was writing my own controls from scratch that needed to look different in Aero vs Luna etc., or (b) my assembly had a generic.xaml file? And in either of those cases, the defaults won't work and I need the attribute?
Joe White
Yes you're on the right track here, but I as we're talking about your own controls, there aren't any 'default' resources. The only defaults you might have arrive via the 'generic.xaml' file. Resources in that dictionary are overridden by theme-specific resource dictionary entries, if they exist.
Drew Noakes