【问题标题】:How to apply different custom themes(resource dictionaries) to the whole application using WPF?如何使用 WPF 将不同的自定义主题(资源字典)应用于整个应用程序?
【发布时间】:2012-02-15 19:48:26
【问题描述】:

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

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。

【问题讨论】:

    标签: wpf dynamic themes resourcedictionary


    【解决方案1】:
    ResourceDictionary skin = new ResourceDictionary();
    skin.Source = new Uri("Themes\\ExpressionLight.xaml", UriKind.Relative);
    Application.Current.Resources.MergedDictionaries.Clear();
    Application.Current.Resources.MergedDictionaries.Add(skin);
    

    【讨论】:

      猜你喜欢
      • 2017-12-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多