SharedResourceDictionary:在Blend中编辑资源
为了优化我的应用程序,我创建了一个SharedResourceDictionary。 有了这个,我在运行时节省了大约250 MB的内存,所以它运行良好。
我的问题出在设计时,在Blend 4中,我无法访问我的资源。 要修改模板(女巫在我的资源文件中),我通常右键单击我的控件,然后选择编辑模板 – >编辑当前,如下图所示:
我需要修改我的模板,它比我的资源文件快100倍,找到好的…我有很多资源……
我的SharedResourceDictionary是这样实现的(在网上找到):
public class SharedResourceDictionary : ResourceDictionary { /// /// Internal cache of loaded dictionaries /// public static Dictionary _sharedDictionaries = new Dictionary(); /// /// Local member of the source uri /// private Uri _sourceUri; /// /// Gets or sets the uniform resource identifier (URI) to load resources from. /// 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]); } } } /// /// Check if we are in Blend to prevent error /// public bool IsInDesignMode { get { return (bool) DependencyPropertyDescriptor.FromProperty(DesignerProperties.IsInDesignModeProperty, typeof(DependencyObject)).Metadata.DefaultValue; } } }
有人知道是否有解决方案吗? 由于内存改进,我真的想保留这个共享字典,但我也想轻松修改我的资源…
任何线索将不胜感激。 谢谢。
编辑:
这样做(SharedResourceDictionary),我也有静态资源的问题:
Provide value on 'System.Windows.Markup.StaticResourceHolder' threw an exception.
当我将它们更改为动态资源时,它可以工作,或者如果我通过简单的ResourceDictionary更改SharedResourceDictionary,它也可以工作。 该项目可以编译,但我无法修改资源,如我所说。
你在为.NET 4编译吗?
您可能遇到无法正确扫描ResourceDictionaries的错误。
有人说你必须在App.xaml级别(在Application.Resources)级别添加ResourceDictionaries,并添加一个虚拟默认样式。
这可能对您不起作用,因为您似乎在控件中添加了资源。
将合并字典添加到合并字典
无法引用包含合并字典的资源字典
实际上使用此function是有问题的。 =(
也许将来事情会变得更好,不知道是否会有所帮助,但你可以尝试不同的实现: WPF SharedResourceDictionary (但不保证它会起作用)
我放弃使用SharedResourceDictionary因为设计时出现问题,只有当项目100%完成时我才会回来使用。