关于不存在的现有资源WPF的警告

时间:2013-12-05 14:13:30

标签: c# wpf dynamicresource

我正在动态加载ResourceDictionary并将其添加到MergedDictionaries,如下所示:

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);

Bright.xaml如下所示:

<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>

这些图像正在UI中显示的树视图中使用:

<Image x:Name="iIcon" Width="16" Height="16" Margin="0, 1, 3, 1" Source="{DynamicResource FolderItemImage}"/>

通常,它们显示没有问题,但是当我运行程序时(尽管图像显示正确),我收到很多警告:

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'

为什么会这样?

2 个答案:

答案 0 :(得分:0)

在加载ResourceDictionary 之前,可能会发生这些错误......一旦加载,它们会停止吗?如果是这种情况,那么你可以忽略它们......毕竟,它们只是警告。

我与Binding有类似的情况,但我可以设置一个Binding.IsAsync属性,告诉Binding值不会立即出现。这使得警告消失了。不幸的是,我认为Resources没有类似的属性,因此您可能只需要使用它,或者尝试提前加载ResourceDictionary

答案 1 :(得分:0)

类似的问题得到了回答&#34;几年前在this question

很遗憾&#34;回答&#34;这意味着微软还没有修复WPF的问题。