SharedResourceDictionary:在混合编辑资源编辑、资源、SharedResourceDictionary

2023-09-03 12:27:41 作者:像拥有

要优化我的申请,我创建了一个SharedResourceDictionary。有了这个,我节省了内存大约250 MB在运行时,这样效果很好。

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

我的问题是在设计时,在Blend 4,我没有更多的访问我的资源。要修改方案(女巫是我的资源文件),我通常用鼠标右键单击我的控制,我选择编辑模板 - >编辑当前,像在这个形象:

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:

我需要修改我的模板的方式,它的100倍的速度,然后走在我的资源文件,并找到好的...我有资源,有很多...

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...

我的SharedResourceDictionary是这样实现的(在网络上找到):

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...

任何线索将AP preciated。谢谢你。

Any clue will be appreciated. Thank you.

编辑:

这样做(SharedResourceDictionary),我也有一个问题,静态资源:

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

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

当我将其更改为动态资源它的工作原理,如果我用一个简单的ResourceDictionary改变SharedResourceDictionary它也适用。该项目可以编译,但我不能编辑的资源没有修改,因为我说。

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.

推荐答案

您编译为.NET 4?

Are you compiling for .NET 4?

您可能会击中它无法扫描ResourceDictionaries正确的bug。

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

有传言,你必须添加ResourceDictionaries在的App.xaml中水平(在Application.Resources)级别,并添加一个虚拟的默认样式。

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合并字典到合并字典

Trouble引用包含合并字典资源字典

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