2

I have a requirement to load and merge ResourceDictionaries in code at runtime according to certain configuration options. Merging the dictionaries in the Application.OnStartup method works fine of course, at runtime.

Visual studio's design mode doesn't load your custom Application class, and the only way I know of to get the dictionaries through in DesignMode is by merging them in App.xaml.

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="SomeDictionary1.xaml" />
            <ResourceDictionary Source="SomeDictionary2.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

This however is not viable in my current situation. So what I'm asking is, what does visual studio actually do to load the merged dictionaries from App.xaml to the design surface at the application scope without constructing your custom Application class? And how can I merge these dictionaries from code to application scope in design mode to actually solve the problem?

leppie
  • 115,091
  • 17
  • 196
  • 297
mrahhal
  • 3,322
  • 1
  • 22
  • 41

1 Answers1

1

Using d: namespace and the mc:Ignorable, this is possible. At design time, it will apply the Source attribute. At runtime, it will ignore it. (you will have an empty ResourceDictionary created at runtime...)

<Application x:Class="WpfApplication2.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
         mc:Ignorable="d"
         StartupUri="MainWindow.xaml">
  <Application.Resources>
    <ResourceDictionary>
      <ResourceDictionary.MergedDictionaries>
          <ResourceDictionary d:Source="designTime.xaml" />
      </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
  </Application.Resources>
</Application>

Thanks, Rob

Rob Relyea
  • 401
  • 4
  • 3
  • Thanks, but I need to do that from code, I'm not sure if that's possible but I think it should. I need that because I want to automate the procedure of adding these dictionaries from code inside a custom Application class, and thus not having to touch the App.xaml. – mrahhal Jan 08 '15 at 18:18
  • And there's actually a problem with "d:Source", it's not working, "Source is not attachable to elements of type ResourceDictionary". – mrahhal Jan 08 '15 at 18:25
  • If you want it to work at design time, you need to do it declaratively, not from code. I thought you wanted to do it from code for runtime, but it had the downside of not being able to work in the designer. The solution I show supports design time use a style that can be a placeholder...at runtime you use the same RD programmatically or have code choose the proper one. Sorry if it doesn't solve your scenario completely. – Rob Relyea Jan 09 '15 at 19:53
  • So there's no way. Alright, this solves my problem just enough actually, but as I said, "d:Source" is telling me that "Source is not attachable to elements of type ResourceDictionary". Of course I added the xmlns. I'm using VS2013. Do I have any idea what's the problem? – mrahhal Jan 10 '15 at 04:25