XAML styles in external assembly

By Mirek on (tags: resources, styles, WPF, XAML, categories: architecture)

Today I will show you how to use the XAML styles located in resource dictionary in external assembly. I will also try to point out the common problems that can raise during this process.

We start with one project WPF application and the resource dictionary included in main window dictionary.

   1: <Window x:Class="WpfApplication.MainWindow"
   2:         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   3:         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
   4:         Title="MainWindow">
   5:     <Window.Resources>
   6:         <Style>
   7:         <!--Some styles here--> 
   8:         </Style>
   9:     </Window.Resources>
  10:     <Grid>
  11:        <!--some controls here-->
  12:     </Grid>
  13: </Window>

and we want to extract the styles from section Window.Resources, place them in separate dictionary file and in separate assembly. First we add a class library project to our solution and name it for example CustomStyles. Then we add an xml file to this library and name it for example Styles.xaml. Our solution now looks similar to this

externalXAML 

Then we copy whole styles from MainWindow.xaml to the Styles.xaml and place it in ResourceDictionary element

   1: <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
   2:                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
   3:    <!--All styles here-->
   4: </ResourceDictionary>

Then in MainWindow.xaml we reference the styles

   1: <Window.Resources>
   2:     <ResourceDictionary>
   3:         <ResourceDictionary.MergedDictionaries>
   4:             <ResourceDictionary Source="pack://application:,,,/CustomStyles;component/Styles.xaml" />
   5:         </ResourceDictionary.MergedDictionaries>
   6:     </ResourceDictionary>
   7: </Window.Resources>

The syntax of the source value is determined by The Pack URI Theme. The only thing to do is now to add to the main application the reference of CustomStyles assembly.

Problems you might encounter:

  1. There is an error in CustomAssembly build which says “Library project file cannot specify ApplicationDefinition element.”
    Solution:
    Check the BuildAction of the Styles.xaml file. It should be set to Page. Sometimes, when you copy files between projects Visual Studio automatically changes its build action. You can change the build action of the file by clicking the right button on it and choosing Properties.
  2. During execution of the wpf application following error occurs: “Cannot locate resource …”
    Solution: Check the resource assembly is properly referenced in the application project. Check the pack uri for the misspelling. It should be in format pack://application:,,,/ReferencedAssembly;component/Subfolder/ResourceFile.xaml where pack://application:,,,/ and component are constant part. If you place you xaml file in a subfolder then you must include it in the uri as shown above.

Cheers