2012-02-23 123 views
0

我想爲我的應用程序定義多個主題,並在每次喜歡時切換它們,但我想將每個主題的每個控件的樣式放在單獨的ResourceDictionary中,以便使文件具有務實的風格,並且我可以快速輕鬆地管理它們。 但問題是:嵌套資源字典的樣式不適用。 有什麼建議嗎? 謝謝。嵌套資源字典

回答

2

我假定您爲每個控件使用了單獨的資源字典,併爲其他主題重複使用它。 因此,我建議你保持一個資源字典爲每個主題如:Theme1.xaml ..併合並其來在這個主題下你所有的資源字典.. 例如:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
> 
<ResourceDictionary.MergedDictionaries> 
    <ResourceDictionary Source="pack://application:,,,/UrProject;component/Themes/Theme1/Button.xaml"/> 
    <ResourceDictionary Source="pack://application:,,,/UrProject;component/Themes/Theme1/Combobox.xaml" /> 
    <ResourceDictionary Source="pack://application:,,,/UrProject;component/Themes/Theme1/ListBox.xaml" /> 
    <ResourceDictionary Source="pack://application:,,,/UrProject;component/Themes/Theme1/Checkbox.xaml" /> 
</ResourceDictionary.MergedDictionaries> 

您可以添加並將此資源字典移除到您的應用程序以切換主題。希望能幫助到你。 :)

+0

感謝名單,我已經做了同樣的,沒有例外,但風格並不適用。 – Mohsen 2012-02-23 06:04:01

+0

如何將主題應用於應用程序?你可以發佈代碼.. – vimal 2012-02-23 06:17:10

+0

代碼是完全一樣XAML美國的書面,但風格不適應 – Mohsen 2012-02-23 08:26:56

0

您可以將主題應用程序這樣的..

public static void ApplyTheme(string themeName) 
    { 
     if (string.IsNullOrEmpty(themeName) == false) 
     { 
      bool exist = false; 
      string themeFileName = 
       string.Format("/UrProject;component/Styles/{0}{1}", themeName, ".xaml"); 
      theme.Source = new Uri(themeFileName, UriKind.RelativeOrAbsolute); 
      foreach (ResourceDictionary dictionary in Application.Current.Resources.MergedDictionaries) 
      { 
       if (string.Equals(dictionary.Source, themeFileName)) 
       { 
        exist = true; 
        break; 
       } 
      } 
      if (exist == false) 
      { 
       Application.Current.Resources.MergedDictionaries.Add(theme); 
      } 

     } 
    }