【问题标题】:Are you not able to have resources in App.xaml if you merge in resource dictionaries?如果合并资源字典,是否无法在 App.xaml 中拥有资源?
【发布时间】:2011-01-17 20:22:37
【问题描述】:

我们在应用程序 xaml 中附加了 4 个全局资源,这些资源是我们在应用程序中用来维护各种项目状态的类。

这是我们在 app.xaml 中使用它们的方式

<Application.Resources>
    <AMSI:Global x:Key="AMSI.Global"/>
    <eFin:Global x:Key="eFinancials.Global" />
    <eService:Global x:Key="eService.Global" /> 
    <eSite:Global x:Key="eSite.Global" />

    ... a bunch of styles and control templates here...
</Application.Resources>

读完第 23 章后,我决定清理一些东西并创建一些资源字典。

所以,我使用 blend 并创建了两个资源字典,并将所有样式移至它们。 Blend 将我的 xaml 修改为以下内容:

<Application.Resources>
   <ResourceDictionary>
      <AMSI:Global x:Key="AMSI.Global"/>
      <eFin:Global x:Key="eFinancials.Global" />
      <eService:Global x:Key="eService.Global" /> 
      <eSite:Global x:Key="eSite.Global" />

      <ResourceDictionary.MergedDictionaries>
         <ResourceDictionary Source="Resources/ControlStyles.xaml"/>
         <ResourceDictionary Source="Resources/DefaultColors.xaml"/>    
      </ResourceDictionary.MergedDictionaries>
   </ResourceDictionary>
</Application.Resources>

好的……太好了。当时我很开心。直到我尝试运行该应用程序并收到初始化错误。显然,它发生在我们拥有此代码的上述全局对象之一的构造函数中:

var amsiGlobal = Application.Current.Resources["AMSI.Global"] as AMSI.Global;
amsiGlobal.PropertyChanged += new 
  System.ComponentModel.PropertyChangedEventHandler(amsiGlobal_PropertyChanged);

它不再获得对 AMSI.Global 对象的引用...但在移动之前它一直工作正常。

我做错了什么。如果我将这些全局对象或资源字典移到外部,代码会再次运行,但 blend 告诉我 xaml 中有错误,当我尝试创建新的控件模板时,它也找不到字典。此外,如果我创建控件模板,即使它们与引用它的控件位于同一页面上,也似乎找不到它们。所以,我假设在运行时会有一个问题。

有什么想法吗?我是否只需将所有样式和模板移回 app.xaml?

【问题讨论】:

    标签: silverlight-4.0 resources resourcedictionary


    【解决方案1】:

    我对 silverlight 有点陌生,但是,你有没有尝试添加

    <AMSI:Global x:Key="AMSI.Global"/>
      <eFin:Global x:Key="eFinancials.Global" />
      <eService:Global x:Key="eService.Global" /> 
      <eSite:Global x:Key="eSite.Global" />
    

    合并字典? 它看起来像这样

    <Application.Resources>
    

      <ResourceDictionary.MergedDictionaries>
         <ResourceDictionary>
            <AMSI:Global x:Key="AMSI.Global"/>
            <eFin:Global x:Key="eFinancials.Global" />
            <eService:Global x:Key="eService.Global" /> 
            <eSite:Global x:Key="eSite.Global" />
         </ResourceDictionary>
         <ResourceDictionary Source="Resources/ControlStyles.xaml"/>
         <ResourceDictionary Source="Resources/DefaultColors.xaml"/>    
      </ResourceDictionary.MergedDictionaries>
    

    我通常只是将我的资源添加到合并字典中,从来没有遇到过问题

    问候

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多