WPF combined resource dictionaries in each user control - a bad idea?

I save my application resources in a separate DLL and refer to them in my main EXE using something like this in App.xaml: -

<ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="pack://application:,,,/MyThemesAssembly;component/Themes/Generic.xaml"/> </ResourceDictionary.MergedDictionaries> 

When I edit a window that is in the main EXE project, the VS2010 constructor does not recognize any resources from another assembly, so I don’t see the application of any style (this is actually not a problem, since I always work in the XAML View). However, Resharper also does not recognize these external resource names, which leads to a lot of squiggles under the resource names when I edit XAML.

I found that I can fix both the VS constructor and Resharper by including the aforementioned XAML in each window and user control, but will it negatively affect memory and / or performance? Will each window receive a separate copy of the resources?

+7
source share
3 answers

We had a problem in our application using the ResourceDictionaries referenced in each UserControl / View. I advise against this. We managed to reduce the application memory by about 300 mb using SharedResourceDictionaries. It looks like you will end up with a ResourceDictionary created once for each UserControl user in your application. Do not do this to fix the VS constructor.

+3
source

Try using VS2012.

I have a test project that I used with which I ran a resource dictionary, merging with an external assembly and in my .xaml application I have this:

 <Application x:Class="WpfPackDictionaries.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" StartupUri="MainWindow.xaml"> <Application.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="pack://application:,,,/WPFCommonLibrary;component/Vectors/Vectors.xaml"/> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Application.Resources> </Application> 

Then in my mainwindow.xaml I have this when the path draws in the style of ModifiablePathStyle:

 <Window xmlns:Control="clr-namespace:WPF.Common.Control;assembly=WPFCommonLibrary" x:Class="WpfPackDictionaries.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <Grid> <Path Style="{StaticResource ModifiablePathStyle}" Fill="Red"/> <Control:Jabberwocky /> </Grid> </Window> 

Intellisense / Resharper (V7.1 10/31 (Early Access Build)) recognizes the style and I don't have squigglies:

enter image description here

Therefore, you tried to work in VS2012?

+2
source

VS2012 is able to β€œsee” resources because the VS XAML constructor loads and executes your code at design time, so VS can check which resources are available at run time. ReSharper never uses the execution of time code (since it requires your code to always be in a compiled state), so XAML support has become a bit more difficult.

ReSharper 8.0 implemented support for BAML decompilation and extracts a list of XAML files and solves XAML resources from referenced binary assemblies.

+2
source

All Articles