2012-07-03 39 views
9

爲了優化我的應用程序,我創建了一個SharedResourceDictionary。藉此,我可以在運行時節省大約250 MB的內存,所以它運行良好。SharedResourceDictionary:在Blend中編輯資源

我的問題是在設計時,在混合4,我沒有更多的訪問我的資源。要修改模板(女巫是在我的資源文件),我通常用鼠標右鍵單擊我的控制,我選擇編輯模板 - >編輯當前,這樣的形象:

enter image description here

我需要修改我的模板,這是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也可以。該項目可以編譯,但我不能修改資源沒有修改,如我所說。

+0

我認爲這將是也是很好的參考示例代碼出處:http://www.wpftutorial。net/MergedDictionaryPerformance.html,同樣的源註釋部分也沒有解釋如何在設計時使用共享資源字典。謝謝。 – Ahmad

+0

一種可能的方法來避免這是一個簡單的資源字典時更換SharedResourceDictionary在調試模式,即:#如果DEBUG 公共類SharedResourceDictionary:資源字典 {} 的#else ... <你的SharedResourceDictionary碼> – Cesario

回答

1

你編譯爲.NET 4?

你可能會碰到錯誤,這錯誤失敗正確掃描您的資源詞典

有人說你必須在App.xaml級別(在Application.Resources)級別添加你的ResourceDictionaries,並添加一個虛擬的默認樣式。

這可能不適合你,因爲你似乎正在控制你的資源。

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

+0

這似乎不能解決問題。我仍然遇到使用[此SharedResourceDictionary實現](http://stackoverflow.com/a/10722527/1781068)的問題。 – Gregor

0

實際上,使用此功能存在問題。 =(

也許在將來,事情會變得更好,不知道是否會有所幫助,但你可以嘗試不同的實現: WPF SharedResourceDictionary (但不保證它會工作)

我放棄了使用因爲在設計時的問題SharedResourceDictionary,只有當項目完成100%我會回來使用。