I have a WPF class library project, used by a C++ application.
In my project, I have many user controls that look something like this:
<UserControl x:Class="TheClass"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
[...]
>
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MyLib;Themes/MyBigFatStyles.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
<StackPanel>
<views:CustomControl Style="{Static Resource CustomControlStyle}"/>
<views:CustomControl Style="{Static Resource CustomControlStyle}"/>
<views:CustomControl Style="{Static Resource CustomControlStyle}"/>
<views:CustomControl Style="{Static Resource CustomControlStyle}"/>
<views:CustomControl Style="{Static Resource CustomControlStyle}"/>
</StackPanel>
</UserControl>
Whenever this control is shown, "MyBigFatStyles.xaml" is loaded into memory five times. Closing the container does not release the memory, and showing the control again loads it into memory five more times. Another control will load "MyBigFatStyles.xaml" into memory more than 150 times, and quickly causes an OutOfMemory exception.
My workaround, one I found through searching for a solution, has been to create a static class called "SharedResourceDictionary," to use that instead of ResourceDictionary, and this has greatly reduced the memory footprint into something manageable. But this breaks the visual designer, and also requires changes to every XAML file in every project.
Is there a better way? Other answers have said to put re-used dictionaries in App.xaml, but I understand that's only an option for WPF applications, and here the WPF is in a class library called from a C++ (not CLI) application.