WPF异常:在“System.Windows.Markup.StaticResourceHolder”上提供值时引发了异常。

异常背景:

  第一次开发 WPF 时,所有资源都定义在 App.xaml 文件中。随着项目资源的增多,查看与修改资源时很麻烦,就在 App.xaml 以集成资源字典的方式。

异常原因:

  在 App.xaml 中定义资源时,我把项目中需要使用的 Class 都写在最上面   

<Application.Resources>
  <app:ColorCollection_Column x:key="C_CCC"/>
  ...
</Application.Resources>       
App.xaml

   在 App.xaml 中集成资源字段时,我把 Class 与 其它资源分开

<ResourceDictionary 
    xmlns:app ="clr-namespace:TZCloud"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <app:ColorCollection_Column x:Key="C_CCC"/>
</ResourceDictionary>
资源字典 - Class
<ResourceDictionary 
    xmlns:app ="clr-namespace:TZCloud"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    ...
</ResourceDictionary>
资源字典 - 其它
<Application.Resources>
  <ResourceDictionary >
    <ResourceDictionary.MergedDictionaries>
      <ResourceDictionary Source="Dictionary\Other.xaml"/>
      <ResourceDictionary Source="Dictionary\Class.xaml"/>              
    </ResourceDictionary.MergedDictionaries>
  </ResourceDictionary>
</Application.Resources>   

  因为 Class 资源声明在后,而其它资源声明(该资源中有对 Class 资源的引用 )在前,所以报错。


 

posted @ 2016-04-22 16:27  Me_Code  阅读(3285)  评论(0编辑  收藏  举报