我们在应用程序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吗?
答案 0 :(得分: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>
我通常只是在合并的词典中添加我的资源,从来没有遇到过问题
此致