2012-02-23 119 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); 
      } 

     } 
    }