I'm dynamically loading a ResourceDictionary
and adding it to MergedDictionaries
like the following:
var mergedDictionaries = Resources.MergedDictionaries;
mergedDictionaries.Clear();
// Generic styles
ResourceDictionary vsStyles = new ResourceDictionary();
vsStyles.Source = new Uri("pack://application:,,,/AssemblyName;component/VSTheme/VSStyles.xaml");
mergedDictionaries.Add(vsStyles);
// Theme-dependent styles
ResourceDictionary bright = new ResourceDictionary();
bright.Source = new Uri("pack://application:,,,/AssemblyName;component/Images/Bright.xaml");
mergedDictionaries.Add(bright);
The Bright.xaml looks like the following:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<BitmapImage UriSource="..\Images\Bright\folder-bright.png" x:Key="FolderItemImage" />
<BitmapImage UriSource="..\Images\Bright\class-bright.png" x:Key="ClassItemImage" />
(...)
</ResourceDictionary>
These images are being used in treeviewitems displayed in the UI:
<Image x:Name="iIcon" Width="16" Height="16" Margin="0, 1, 3, 1" Source="{DynamicResource FolderItemImage}"/>
Generally, they display without problems, but when I run the program (despite images being displayed correctly), I receive a lot of warnings:
System.Windows.ResourceDictionary Warning: 9 : Resource not found; ResourceKey='NativeImage'
System.Windows.ResourceDictionary Warning: 9 : Resource not found; ResourceKey='ClassItemImage'
System.Windows.ResourceDictionary Warning: 9 : Resource not found; ResourceKey='NativeImage'
System.Windows.ResourceDictionary Warning: 9 : Resource not found; ResourceKey='ClassItemImage'
Why is it so?
Those errors could just be happening before the
ResourceDictionary
is loaded... do they stop once it has been loaded? If that is the case, then you can just ignore them... after all, they are just warnings.I had a similar situation with
Binding
s, but there is aBinding.IsAsync
property that I could set that tells theBinding
that values will not come immediately. This made the warnings disappear. Unfortunately, I don't think that there is a similar property forResources
, so you may just have to live with it, or try to load yourResourceDictionary
earlier.