Reputation: 3117
We have 4 global resources attached in app xaml that are classes we use in our application to maintain state for various items.
Here is how we have them in 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>
After reading chapter 23 I decided I was going to clean some stuff up and create few resource dictionaries.
So, I used blend and created two resource dictionaries and moved all the styles to them. Blend modifyied my xaml to the following:
<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>
Ok... great. I was happy. Until I tried to run the app and got an initialization error. Apparently it happens in the constructor of one of the above global objects where we have this code:
var amsiGlobal = Application.Current.Resources["AMSI.Global"] as AMSI.Global;
amsiGlobal.PropertyChanged += new
System.ComponentModel.PropertyChangedEventHandler(amsiGlobal_PropertyChanged);
It was no longer getting a reference to the AMSI.Global object... but it had been working fine before the move.
What am I doing wrong. If I move those global objects outside or the resource dictionaries the code runs again but blend tells me there is an error in the xaml, it also doesn't find the dictionaries when I try to create new control templates. Also, if I create control templates they don't seem to be found, even if they are on the same page as the control referencing it. So, I assume at runtime there will be an issue to.
Any ideas what is going on here? Do I just need to move all my styles and templates back to app.xaml?
Upvotes: 0
Views: 1264
Reputation: 25
I'm a bit new at silverlight, but, have you tried adding
<AMSI:Global x:Key="AMSI.Global"/>
<eFin:Global x:Key="eFinancials.Global" />
<eService:Global x:Key="eService.Global" />
<eSite:Global x:Key="eSite.Global" />
to the Merged Dictionaries? it would look something like this
<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>
I normally just add my resources in the merged dictionaies and never had a problem
Regards
Upvotes: 1