locked
How to load the same ResourceDictionary twice? RRS feed

  • Question

  • Please look at this simple code:

     

    Code Block

    ResourceDictionary aSkinDictionary = new MyDictionary()

     

    Collection<ResourceDictionary> mergedDicts = this.Resources.MergedDictionaries;

     

    mergedDicts.Add(aSkinDictionary);

    mergedDicts.Clear();

    mergedDicts.Add(aSkinDictionary); // At this line runtime error: The merged dictionary is invalid. Either a ResourceDictionary is being placed into its own MergedDictionaries collection or a it is being added to the same MergedDictionary collection twice.

     

     

     

    Question 1: Is it possible to use the same resource dictionary twice? What I need to do to avoid this error?

     

    Question 2: It looks like simple clearing of collection of merged directories doesn't help. Does it mean that somethig is left in the memory after I clean collection of merged dictionaries? Memory leaks??? 

     

    Thanks!

    Monday, December 10, 2007 8:29 PM

Answers

  • Hi Ivan

     

    I think that you can use Remove instead of Clear method, for example,

     

    Code Block

    foreach (ResourceDictionary rd in this.Resources.MergedDictionaries)

    {

        this.Resources.MergedDictionaries.Remove(res);

    }

     

     

    Best Regards,

    Wei Zhou

    Wednesday, December 12, 2007 6:10 AM

All replies

  •  

    Okey... probably somebody will find this usefull. I didn't solve this problem Sad so, I worked around this problem by creating new resource dictionary of same type.

     

    Code Block

    Collection<ResourceDictionary> mergedDicts = this.Resources.MergedDictionaries;

     

    ResourceDictionary aSkinDictionary1 = new MyDictionary();

    mergedDicts.Add(aSkinDictionary1);

     

    mergedDicts.Clear();

     

    ResourceDictionary aSkinDictionary2 = new MyDictionary();

    mergedDicts.Add(aSkinDictionary2); // No error!

     

     

    But unfortunatelly, this solution leads to memory leaks...

     

    Do we have a professional .Net developer here in forum who knows the internals of WPF? Will aSkinDictionary1 be garbage collected or won't?

     

    Thanks again!

    Monday, December 10, 2007 10:35 PM
  • Perhaps you could wrap the ResourceDictionary in a Collection. Then rather than Clear(), you can swap Collections:

     

    Code Block

     

    ResourceDictionary dict1 = new ResourceDictionary();

    Collection< ResourceDictionary > dicts = Resources.MergedDictionaries;

     

    // Wrap my ResourceDictionary in a Collection.

    Collection< ResourceDictionary > collection1 = new Collection< ResourceDictionary >();

    collection1.Add( dict1 );

     

    // Swap between Collections.

    dicts = collection1;

    dicts = Resources.MergedDictionaries;

    dicts = collection1;

     

     

     
     
    Tuesday, December 11, 2007 3:04 AM
  •  

    Unfortunately it is impossible to assign whole collection to Resources.MergedDictionaries (it is read only property). As I understand you idea, the code should be like this:

     

    Code Block

    public void ApplySkin()

    {

    Collection<ResourceDictionary> dicts = Resources.MergedDictionaries;

    ResourceDictionary dict1 = new ResourceDictionary();

    dicts.Add(dict1);

     

    // Wrap

    Collection<ResourceDictionary> collection1 = new Collection<ResourceDictionary>();

    collection1.Add(dict1);

     

    Resources.MergedDictionaries = collection1; // Error 1 Property or indexer 'System.Windows.ResourceDictionary.MergedDictionaries' cannot be assigned to -- it is read only

    }

     

     

    but compiler doesn't like it Sad

     

    Wednesday, December 12, 2007 1:18 AM
  • Hi Ivan

     

    I think that you can use Remove instead of Clear method, for example,

     

    Code Block

    foreach (ResourceDictionary rd in this.Resources.MergedDictionaries)

    {

        this.Resources.MergedDictionaries.Remove(res);

    }

     

     

    Best Regards,

    Wei Zhou

    Wednesday, December 12, 2007 6:10 AM