title: Resources not resolving from ResourceDictionaries
body: <p>I'm having problems resolving Resources from my ResourceDictionaries.</p>
  
  <p>I decided to refactor my rather large ResourceDictionary into individual dictionary files, organized into subfolders.</p>
  
  <p>I have a ResourceLibrary.xaml under Resources:</p>
  
  <pre><code>&lt;ResourceDictionary x:Class="MyProject.Resources.ResourceLibrary"
                  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"&gt;
     &lt;ResourceDictionary.MergedDictionaries&gt;
       &lt;!-- Colours --&gt;
       &lt;ResourceDictionary Source="Colors/ConnectedCellColor.xaml" /&gt;
       &lt;!-- Brushes --&gt;
       &lt;ResourceDictionary Source="Brushes/ConnectorCellBrush.xaml" /&gt;
       &lt;!-- Control Templates --&gt;
       &lt;ResourceDictionary Source="ControlTemplates/ConnectorCellTemplate.xaml" /&gt;
       &lt;!-- Base Styles --&gt;
       &lt;ResourceDictionary Source="BaseStyles/ConnectorBaseStyle.xaml" /&gt;
     &lt;/ResourceDictionary.MergedDictionaries&gt;
  &lt;/ResourceDictionary&gt;
  </code></pre>
  
  <p>The class is there for a reason, in the code behind, I can add [Export(typeof (ResourceDictionary))] so MEF can find it.</p>
  
  <p>I have a View: (simplified)</p>
  
  <pre><code>&lt;UserControl x:Class="MyProject.ConnectorCellView"
           Style="{StaticResource ConnectorBaseStyle}"
  &lt;/UserControl&gt;
  </code></pre>
  
  <p>ConnectorBaseStyle:</p>
  
  <pre><code>&lt;ResourceDictionary&gt;
     &lt;Style x:Key="ConnectorBaseStyle" TargetType="UserControl"&gt;
        &lt;Setter Property="Template" Value="{StaticResource ConnectorCellTemplate}" /&gt;
     &lt;/Style&gt;
  &lt;/ResourceDictionary&gt;
  </code></pre>
  
  <p>The template has StaticResources to try and get a Brush and a colour.</p>
  
  <p>All of these StaticResources will not resolve any more.</p>
  
  <p>I thought it might have been an order issue, but since these resources are contained in a plugin to my main program, I use MEF and ImportMany to get all the exported ResourceDictionaries, and in my Caliburn.Micro bootstrapper:</p>
  
  <pre><code>  public void OnImportsSatisfied()
    {
       foreach (ResourceDictionary resourceDictionary in ResourceDictionaries)
       {
          Application.Resources.MergedDictionaries.Add(resourceDictionary);
       }
    }
  </code></pre>
  
  <p>(Neat trick I found somewhere)</p>
  
  <p>I can actually run my program, and when that view is created it throws an exception when trying to set the style:</p>
  
  <p>System.InvalidCastException<br>
  Unable to cast object of type 'MS.Internal.NamedObject' to type System.Windows.FrameworkTemplate'. </p>
  
  <p>The only information I've found relating to this has to do with the order resources are defined, but from the order I have them in ResourceLibrary, it should work.</p>
  
  <p>When the exception is thrown, I can examine Application.Current.Resources.MergedDictionaries,
  and see the resources. </p>
  
  <p>I've tried various ways of specifying the Source in ResourceLibrary</p>
  
  <pre><code>&lt;ResourceDictionary Source="/MyProject;component/Resources/BaseStyles/ConnectorBaseStyle.xaml" /&gt;
  </code></pre>
  
  <p>etc, no effect on finding them. These resources are only used by the plugin code.</p>
  
  <p>The only thing that seemed to work was changing all the StaticResources to DynamicResources</p>
  
  <p>which doesn't make sense to me, if it is an order issue, why would Static work when they're all in the same file?</p>
  
  <p>Some of my styles used BasedOn, and they don't work with DynamicResource.</p>
  
  <p>Can you help me understand why this is happening, and how to make it work?</p>