As with many people who have tried to organize the resource dictionaries that are used by silverlight, I also got the error “Cannot find a Resource with the Name/Key Foo”, where Foo obviously is the name of the resource one of my styles was referencing.
This is how I wanted to organize my resource dictionaries
- GlobalStyles.xaml
- Control1Styles.xaml (references items in GlobalStyles.xaml)
- Control2Styles.xaml (references items in GlobalStyles.xaml)
The problem was that references in Control1Styles.xaml and Control2Styles.xaml were not getting resolved and I was getting this error. After a couple of minutes of Googling I came on this article with suggested the solution.
Since my solution had other dictionaries as well, this is how my final structure ended up looking like.
- App.xaml
- Styles.xaml
- GlobalStyles.xaml
- Control1Styles.xaml
- Control2Styles.xaml
- OtherDictionaries
- Styles.xaml
This can be interpreted as App.xaml contained definition for Styles.xaml and other dictionaries. Some sample code doing that looks like the code below.
1: <Application.Resources>
2: <ResourceDictionary>
3: <ResourceDictionary.MergedDictionaries>
4: <ResourceDictionary Source="Resources/Styles/Styles.xaml"/>
5: . 6: . 7: .8: </ResourceDictionary.MergedDictionaries>
9: </ResourceDictionary>
10: </Application.Resources>
Moreover, each of my Control1Styles .xaml and Control2Styles.xaml, now contain a merge with the GlobalStyles.xaml dictionary at the top of the file as shown below.
1: <ResourceDictionary.MergedDictionaries>2: <ResourceDictionary Source="GlobalStyles.xaml" />
3: </ResourceDictionary.MergedDictionaries>This structure seemed to resolve the issue. Another thing to that I noted in the MSDN article was that if a resource is defined multiple times in multiple merged dictionaries then the resource that is returned will come from the last dictionary found sequentially in the MergedDictionaries collection.
Hence here are a couple of points to keep in mind.
- Reuse and Maintainability: Bring your reused resources into a separate dictionary rather than declaring it at different places.
- Performance: In this article you can read how “each time a control references a ResourceDictionary in XAML, it creates a new instance of it.” Shocking ? Right. So what you can do is to create ResourceDictionaries for different controls as I have illustrated above and try to be as discreet as possible. Not only would it help in someone later to easily figure out where all the styles are but also since your dictionary is just for that control (and hence hopefully very small), you should be faster than if you would be using one gynormous dictionary for the whole app or page or view.
- Programmatic Management of Resource Dictionaries and their scope: You can also go the programmatic route as explained by this article but I have not tested if this is working for Silverlight and might also not be feasible for not very large solutions.
Helpful Articles:
0 comments:
Post a Comment