title: Set up application resources from code
body: <p>I have a c# project that was a WPF application but I now want to build it as a dll. I have previously done this by removing the app.xaml from the project and setting its build type to dll. </p>
  
  <p>The issue I have now is that the app.xaml contains some xaml to instantiate the application variables. To get round this I am trying to programmatically set these application variables from within the first xaml window that will be called. </p>
  
  <p>The xaml I am trying to emulate in code is:</p>
  
  <pre><code>&lt;Application.Resources&gt;
      &lt;ResourceDictionary&gt;
        &lt;ResourceDictionary.MergedDictionaries&gt;
          &lt;ResourceDictionary Source="Resources/Styles/Shared.xaml"/&gt;
          &lt;ResourceDictionary Source="Resources/Styles/ToolBar.xaml"/&gt;
          &lt;ResourceDictionary Source="Resources/Styles/GroupBox.xaml"/&gt;
          &lt;ResourceDictionary Source="Resources/Styles/ZoomBox.xaml"/&gt;
          &lt;ResourceDictionary Source="Resources/Styles/ScrollBar.xaml"/&gt;
          &lt;ResourceDictionary Source="Resources/Styles/Expander.xaml"/&gt;
          &lt;ResourceDictionary Source="Resources/ApplicationToolbar.xaml"/&gt;
          &lt;ResourceDictionary Source="Resources/DesignerItem.xaml"/&gt;
          &lt;ResourceDictionary Source="Resources/Styles/ToolboxItem.xaml"/&gt;
          &lt;ResourceDictionary Source="Resources/Styles/Toolbox.xaml"/&gt;
          &lt;ResourceDictionary Source="Resources/Connection.xaml"/&gt;
          &lt;ResourceDictionary Source="Resources/Slider.xaml"/&gt;
          &lt;ResourceDictionary Source="Resources/ScrollViewer.xaml"/&gt;
          &lt;ResourceDictionary Source="Resources/StatusBar.xaml"/&gt;
          &lt;/ResourceDictionary.MergedDictionaries&gt;
      &lt;/ResourceDictionary&gt;
    &lt;/Application.Resources&gt;
  </code></pre>
  
  <p>This is the code I have:</p>
  
  <pre><code>ResourceDictionary myResourceDictionary = new ResourceDictionary();
              myResourceDictionary.Source = new Uri("C:\\Resources\\Styles\\Shared.xaml");
              Application.Current.Resources.MergedDictionaries.Add(myResourceDictionary);
              myResourceDictionary.Source = new Uri("C:\\Resources\\Styles\\ToolBar.xaml");
              Application.Current.Resources.MergedDictionaries.Add(myResourceDictionary);
              myResourceDictionary.Source = new Uri("C:\\Resources\\Styles\\GroupBox.xaml");
              Application.Current.Resources.MergedDictionaries.Add(myResourceDictionary);
              myResourceDictionary.Source = new Uri("C:\\Resources\\Styles\\ZoomBox.xaml");
              Application.Current.Resources.MergedDictionaries.Add(myResourceDictionary);
              myResourceDictionary.Source = new Uri("C:\\Resources\\Styles\\ScrollBar.xaml");
              Application.Current.Resources.MergedDictionaries.Add(myResourceDictionary);
              myResourceDictionary.Source = new Uri("C:\\Resources\\Styles\\Expander.xaml");
              Application.Current.Resources.MergedDictionaries.Add(myResourceDictionary);
              myResourceDictionary.Source = new Uri("C:\\Resources\\ApplicationToolbar.xaml");
              Application.Current.Resources.MergedDictionaries.Add(myResourceDictionary);
              myResourceDictionary.Source = new Uri("C:\\Resources\\DesignerItem.xaml");
              Application.Current.Resources.MergedDictionaries.Add(myResourceDictionary);
              myResourceDictionary.Source = new Uri("C:\\Resources\\Styles\\ToolboxItem.xaml");
              Application.Current.Resources.MergedDictionaries.Add(myResourceDictionary);
              myResourceDictionary.Source = new Uri("C:\\Resources\\Styles\\Toolbox.xaml");
              Application.Current.Resources.MergedDictionaries.Add(myResourceDictionary);
              myResourceDictionary.Source = new Uri("C:\\Resources\\Connection.xaml");
              Application.Current.Resources.MergedDictionaries.Add(myResourceDictionary);
              myResourceDictionary.Source = new Uri("C:\\Resources\\Slider.xaml");
              Application.Current.Resources.MergedDictionaries.Add(myResourceDictionary);
              myResourceDictionary.Source = new Uri("C:\\Resources\\ScrollViewer.xaml");
              Application.Current.Resources.MergedDictionaries.Add(myResourceDictionary);
              myResourceDictionary.Source = new Uri("C:\\Resources\\StatusBar.xaml");
              Application.Current.Resources.MergedDictionaries.Add(myResourceDictionary);
  </code></pre>
  
  <p>Should this work?</p>
  
  <p>I'm hitting a problem in that Toolbar.xaml references a resource declared in Shared.xaml but its not getting picked up and im getting the following error. </p>
  
  <pre><code>Cannot find resource named 'ToolbarSelectedBackgroundBrush'. Resource names are case sensitive.
  </code></pre>
  
  <p>Here is where the resource is delcared in shared.xaml</p>
  
  <pre><code>&lt;LinearGradientBrush x:Key="ToolbarSelectedBackgroundBrush" StartPoint="0,0" EndPoint="0,1"&gt;
      &lt;GradientBrush.GradientStops&gt;
        &lt;GradientStopCollection&gt;
          &lt;GradientStop Color="#FFFEE3" Offset="0.0"/&gt;
          &lt;GradientStop Color="#FFE797" Offset="0.4"/&gt;
          &lt;GradientStop Color="#FFD750" Offset="0.4"/&gt;
          &lt;GradientStop Color="#FFE796" Offset="1.0"/&gt;
        &lt;/GradientStopCollection&gt;
      &lt;/GradientBrush.GradientStops&gt;
    &lt;/LinearGradientBrush&gt;
  </code></pre>
  
  <p>and here's where its referenced in toolbar.xaml</p>
  
  <pre><code>&lt;Setter TargetName="Border" Property="Background" Value="{StaticResource ToolbarSelectedBackgroundBrush}" /&gt;
  </code></pre>
  
  <p>Sorry for the essay of a question but thought id provide as much info as I could. Let me know if you need anything else.</p>