如何使用WPF将不同的自定义主题(资源字典)应用到整个应用程序?

发布于 2025-01-06 12:43:41 字数 1042 浏览 4 评论 0原文

我想动态更改整个应用程序的自定义主题。主题以资源字典的形式呈现,称为 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。

I want to dynamically change custom theme for the whole application. Themes are presented as resource dictionaries called ExpressionDark.xaml and ExpressionLight.xaml (downloaded from Codeplex). I'm using combo box to select appropriate theme. Theme changing is happening at SelectionChanged event. Here's the code:

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;
}

This works fine for the current window, but when I run an instance of another application window an XamlParseException occurs.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一百个冬季 2025-01-13 12:43:42
ResourceDictionary skin = new ResourceDictionary();
skin.Source = new Uri("Themes\\ExpressionLight.xaml", UriKind.Relative);
Application.Current.Resources.MergedDictionaries.Clear();
Application.Current.Resources.MergedDictionaries.Add(skin);
ResourceDictionary skin = new ResourceDictionary();
skin.Source = new Uri("Themes\\ExpressionLight.xaml", UriKind.Relative);
Application.Current.Resources.MergedDictionaries.Clear();
Application.Current.Resources.MergedDictionaries.Add(skin);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文