Search code examples
c#silverlightxamlresourcedictionary

C# Silverlight - When do Resource Dictionaries get created?


I am working on a Silverlight 4 project where I get a memory leak because every time some xaml views are loaded they create their own instance of a class that then gets hooked with event triggers and don't get garbage collected. Removing the event listeners has proved too tricky under the circumstances for me to handle it that way. So Instead I want the instance of the class to be re-used every time the xaml view loads.

To do this I was planning on adding a Resource Dictionary to each project that this error is occurring in. My concern though is that I don't know when this Resource Dictionary will be created. When the solution first loads will it load the project then the resource dictionary, immediately creating the class for use? Or will it wait until something asks for it and then create it. Will what actually happens create a copy of the class that gets re-used by everyone that accesses it(as in not another copy)?

I also realize I can place the resources in app.xaml but I need to add many such classes and it will really clutter up the area which shouldn't be bothered with this problem.


Solution

  • Use Christian Moser's SharedResourceDictionary to avoid loading second copies of the dictionary itself or anything it contains. This has saved us a long amount of load time in many controls.