Feed on
Posts
Comments

The <ResourceDictionary.MergedDictionary> tag is a simple way to add external resources to your application.  Imagine that your designer creates some fabulous styles and you want to share them across multiple projects. 

Cut and Paste is sooo out of fashion.  Don’t do it!

Place your styles in an external xaml file or assembly and use the MergedDictionary tag to reference the originals.  The following example adds some resources from the MainResources.xaml file

 

  <Application x:Class="DemoApp"
    xmlns="..."
    xmlns:x="..."   >

    <Application.Resources>
      <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
          <ResourceDictionary Source="Workshops/MainResources.xaml"/>
        </ResourceDictionary.MergedDictionaries>     </ResourceDictionary>
    </Application.Resources>
  </Application>

Adding local application resources

Even though it is easy to add external resources it’s likely that you’ll want to have a style or other resource that you don’t intend to shared across applications.  You could isolated these resources within a single page (Page.Resources) or element (DockPanel.Resources)  But what if you want to add  them to <Application.Resources> tag?  You can do it as long as you know the correct location. They have to go between the </ResourceDictionary.MergedDictionaries> and </ResourceDictionary> tags.

    <Application.Resources>
      <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
          <ResourceDictionary Source="WorkshopHelpers/MainResources.xaml"/>
        </ResourceDictionary.MergedDictionaries>
        <!-- Place here...-->
        <Style TargetType="Rectangle" >
          <Setter Property="Width" Value="100"/>
          <Setter Property="Height" Value="80"/>
        </Style>
      </ResourceDictionary>
    </Application.Resources>
  </Application>

 -Walt Ritscher

RSS Like this article? Subscribe to the RSS feed.

6 Responses to “Mixing external MergedDictionaries with local resources in App.xaml”

  1. manish gour says:

    Thanks for the information…Not knowing where to put the resources entries troubled me a lot.

  2. Mennowar says:

    It’s funny.
    In SP1 the implementation was somehow broken, Beta 2 does not even know the MergedDictionaries – although it’s mentioned in the Help.
    Seems as if there is no really Clean way to hold the Styles and Templates in an external file.

  3. iyalovoi says:

    want to write about it, but find your article first! Good trick.

  4. Kevin Stumpf says:

    Thanks… I did it the other way round first => exception.

  5. Anthony Stavile says:

    How do I reference Default.Xaml that is in a seperate project from the calling project. For example Project A uses Xaml in Project B

    This woun’t work

    • Anthony Stavile says:

      Soon after I posted my question I resolved the issue as follows in the app.xaml: