爲了優化我的應用程序,我創建了一個SharedResourceDictionary。藉此,我可以在運行時節省大約250 MB的內存,所以它運行良好。SharedResourceDictionary:在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;
}
}
}
有人有一個想法,如果噸這是一個解決方案嗎?我真的想保留這個共享字典,因爲內存的改進,但我也想輕鬆修改我的資源...
任何線索將不勝感激。謝謝。
編輯:
這樣做(SharedResourceDictionary),我也有靜態的資源的一個問題:
Provide value on 'System.Windows.Markup.StaticResourceHolder' threw an exception.
當我將其更改爲動態資源它的工作原理,或者如果我改變SharedResourceDictionary通過一個簡單的ResourceDictionary也可以。該項目可以編譯,但我不能修改資源沒有修改,如我所說。
我認爲這將是也是很好的參考示例代碼出處:http://www.wpftutorial。net/MergedDictionaryPerformance.html,同樣的源註釋部分也沒有解釋如何在設計時使用共享資源字典。謝謝。 – Ahmad
一種可能的方法來避免這是一個簡單的資源字典時更換SharedResourceDictionary在調試模式,即:#如果DEBUG 公共類SharedResourceDictionary:資源字典 {} 的#else ... <你的SharedResourceDictionary碼> – Cesario