9

To optimize my application, I create a SharedResourceDictionary. With this, I save about 250 mb of memory at run-time, so it works well.

My problem is in design-time, in Blend 4, I have no more access to my resource. To modify a template (witch is in my resource file), I usually right click on my control and I choose Edit Template --> Edit Current, like in this image:

enter image description here

I need to modify my template that way, it's 100x faster then to go in my resource file and find the good one... I have A LOT of resources...

My SharedResourceDictionary is implemented like this (found on the web):

public class SharedResourceDictionary : ResourceDictionary
{
    /// <summary>
    /// Internal cache of loaded dictionaries 
    /// </summary>
    public static Dictionary<Uri, ResourceDictionary> _sharedDictionaries =
        new Dictionary<Uri, ResourceDictionary>();

    /// <summary>
    /// Local member of the source uri
    /// </summary>
    private Uri _sourceUri;

    /// <summary>
    /// Gets or sets the uniform resource identifier (URI) to load resources from.
    /// </summary>
    public new Uri Source
    {
        get
        {
            if (IsInDesignMode)
                return base.Source;

            return _sourceUri;
        }
        set
        {
            _sourceUri = value;

            if (!_sharedDictionaries.ContainsKey(value))
            {
                // If the dictionary is not yet loaded, load it by setting
                // the source of the base class
                base.Source = value;

                // add it to the cache
                _sharedDictionaries.Add(value, this);
            }
            else
            {
                // If the dictionary is already loaded, get it from the cache
                MergedDictionaries.Add(_sharedDictionaries[value]);
            }
        }
    }

    /// <summary>
    /// Check if we are in Blend to prevent error
    /// </summary>
    public bool IsInDesignMode
    {
        get
        {
            return
            (bool)
            DependencyPropertyDescriptor.FromProperty(DesignerProperties.IsInDesignModeProperty,
            typeof(DependencyObject)).Metadata.DefaultValue;
        }
    }
}

Someone have an idea if there is a solution for this? I really want to keep this shared dictionary due to the memory improvement, but I also want to modify my resource easily...

Any clue will be appreciated. Thank you.

EDIT:

Doing this (SharedResourceDictionary), I also have an issue with static resource:

Provide value on 'System.Windows.Markup.StaticResourceHolder' threw an exception.

And when I change them to dynamic resource it works, or if I change the SharedResourceDictionary by a simple ResourceDictionary it also works. The project can compile but I cannot edit the resource without a modification as I said.

mlemay
  • 1,622
  • 2
  • 32
  • 53
  • I think it would be also good to reference the source of your sample code: http://www.wpftutorial.net/MergedDictionaryPerformance.html , also the same source comment section does explain how to make use of shared resource dictionaries at design time. Thanks. – Ahmad Aug 11 '15 at 07:38
  • A possible way to avoid this is to replace the SharedResourceDictionary with a simple ResourceDictionary when in debug mode, ie.: #if DEBUG public class SharedResourceDictionary : ResourceDictionary { } #else ... – Cesario Feb 20 '17 at 05:50

2 Answers2

2

Are you compiling for .NET 4?

You might be hitting the bug where it fails to scan your ResourceDictionaries properly.

There's talk that you have to add your ResourceDictionaries at the the App.xaml level (in Application.Resources) level, and add a dummy default style.

This might not work for you as you seem to be adding your resources in your control.

http://blogs.windowsclient.net/rob_relyea/archive/2010/04/26/my-staticresource-reference-worked-differently-with-wpf-4-rc-than-it-does-with-wpf-4-rtm.aspx

Adding a Merged Dictionary to a Merged Dictionary

Trouble referencing a Resource Dictionary that contains a Merged Dictionary

http://connect.microsoft.com/VisualStudio/feedback/details/553528/defaultstylekey-style-not-found-in-inner-mergeddictionaries

Community
  • 1
  • 1
Colin Smith
  • 12,375
  • 4
  • 39
  • 47
  • This does not seem to solve the problem. I still experience the issue using [this SharedResourceDictionary implementation](http://stackoverflow.com/a/10722527/1781068). – Gregor Nov 22 '12 at 14:13
0

Actually it is problematic to use this feature. =(

Maybe in the future things will get better, do not know if will help, but you can try a different implementation: WPF SharedResourceDictionary (but do not guarantee it will work)

I gave up using the SharedResourceDictionary because of problems at design time, only when the project is 100% completed I'll be back to use.

Community
  • 1
  • 1
J. Lennon
  • 3,311
  • 4
  • 33
  • 64