2011-05-08 39 views
2

我有2資源詞典資產/橙/ Core.xaml和資產/灰/ Core.xaml。我想要動態加載這些。該字典包含隱式和顯式樣式。我已經嘗試了更改資源字典的標準代碼。代碼: -爲什麼我的資源字典沒有更新外觀?真的受夠了

Uri uri = new Uri("/FMT.Client;component/Assets/Orange/Core.xaml", UriKind.Relative); 
      StreamResourceInfo info = Application.GetResourceStream(uri); 

      if (info != null) 
      { 
       StreamReader reader = new StreamReader(info.Stream); 
       ResourceDictionary currentDict = XamlReader.Load(reader.ReadToEnd()) as ResourceDictionary; 
       reader.Close(); 
       if (currentDict != null) 
       { 
        Application.Current.Resources.MergedDictionaries.RemoveAt(Application.Current.Resources.MergedDictionaries.Count - 1); 
        Application.Current.Resources.MergedDictionaries.Add(currentDict); 

       } 
      } 

我不知道爲什麼我的主題沒有改變。我真的很厭倦,感覺好像撞在牆上。首先我把這個代碼放在特定的頁面上。但它沒有按預期工作。然後我在一些線程上看到,這是另一個SL4的限制,我們需要在頁面加載之前放置代碼。讀這個我把這個代碼放在App.xaml.cs的構造函數中,在行InitializeComponent()後面,但它仍然不起作用。我還能做什麼?我已經花了近3個小時落後於這個愚蠢的無用問題。現在我受不了了。行Application.Current.Resources.MergedDictionaries.Add(currentDict);確實得到執行,但沒有看到主題的變化。

回答

1

原代碼可以適當清除合併的字典 - 假設只有1字典合併。我無法從代碼片段中看出來。

BiggsTRC爲您提供了一個很好的例子。

我相信你缺少的難題是你需要重置所有的靜態資源。刪除舊的合併字典並添加新字典不會更新StaticResource聲明。注意示例的ResetStyles方法。你可能需要這樣的東西。