Warning about non-existing existing resource, WPF

3k views Asked by At

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?

2

There are 2 answers

0
Sheridan On

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 Bindings, but there is a Binding.IsAsync property that I could set that tells the Binding that values will not come immediately. This made the warnings disappear. Unfortunately, I don't think that there is a similar property for Resources, so you may just have to live with it, or try to load your ResourceDictionary earlier.

0
Shakaron On

Similar question got "answered" years ago in this question.

Unfortunately "answered" means that it's a problem with WPF that Microsoft hasn't fixed yet.