2012-02-15 30 views
1

我想动态改变整个应用程序的自定义主题。主题以资源字典的形式呈现,称为ExpressionDark.xaml和ExpressionLight.xaml(从Codeplex下载)。我正在使用组合框来选择适当的主题。 SelectionChanged事件正在发生主题变化。下面的代码:如何使用WPF将不同的自定义主题(资源字典)应用于整个应用程序?

private void themesComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e) 
{ 
ResourceDictionary resourceDictionary = new ResourceDictionary(); 

    int theme = ((ComboBox)sender).SelectedIndex; 

    switch (theme) 
    { 
    case (int)Themes.Dark: 
       resourceDictionary = Application.LoadComponent(
       new Uri(@"Themes\ExpressionDark.xaml", 
       UriKind.Relative)) as ResourceDictionary; 
       break; 
      case (int)Themes.Light: 
       resourceDictionary = Application.LoadComponent(
       new Uri(@"Themes\ExpressionLight.xaml", 
       UriKind.Relative)) as ResourceDictionary; 
       break; 
      default: 
       break; 
    } 

Application.Current.Resources = resourceDictionary; 
} 

也能正常工作为当前窗口,但是当我运行其他应用程序窗口的实例发生XamlParseException。

回答

2
ResourceDictionary skin = new ResourceDictionary(); 
skin.Source = new Uri("Themes\\ExpressionLight.xaml", UriKind.Relative); 
Application.Current.Resources.MergedDictionaries.Clear(); 
Application.Current.Resources.MergedDictionaries.Add(skin); 
+1

谢谢,它的工作原理。 – 2012-02-16 13:54:24

相关问题