Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SharedResourceDictionary: edit resource in Blend

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.

like image 432
mlemay Avatar asked Jul 03 '12 19:07

mlemay


1 Answers

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

like image 104
CSmith Avatar answered Nov 08 '22 01:11

CSmith