SharedResourceDictionary:在Blend中编辑资源

mle*_*may 9 .net c# wpf performance xaml

为了优化我的应用程序,我创建了一个SharedResourceDictionary.有了这个,我在运行时节省了大约250 MB的内存,所以它运行良好.

我的问题出在设计时,在Blend 4中,我无法访问我的资源.要修改模板(女巫在我的资源文件中),我通常右键单击我的控件,然后选择编辑模板 - >编辑当前,如下图所示:

在此输入图像描述

我需要修改我的模板,它比我的资源文件快100倍,找到好的...我有很多资源......

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

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;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

有人知道是否有解决方案吗?由于内存改进,我真的想保留这个共享字典,但我也想轻松修改我的资源...

任何线索将不胜感激.谢谢.

编辑:

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

Provide value on 'System.Windows.Markup.StaticResourceHolder' threw an exception.
Run Code Online (Sandbox Code Playgroud)

当我将它们更改为动态资源时,它可以工作,或者如果我通过简单的ResourceDictionary更改SharedResourceDictionary它也可以工作.该项目可以编译,但我不能修改资源,如我所说.

Col*_*ith 2

您正在针对 .NET 4 进行编译吗?

您可能会遇到无法正确扫描资源字典的错误。

有人说您必须在 App.xaml 级别(在 Application.Resources 中)级别添加 ResourceDictionaries,并添加虚拟默认样式。

这可能对您不起作用,因为您似乎正在将资源添加到您的控制中。

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

将合并词典添加到合并词典

引用包含合并字典的资源字典时出现问题

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