更改 ResourceDictionary 不会明显影响 WPF 中的 Window

发布于 2024-09-14 03:36:06 字数 599 浏览 4 评论 0原文

我有一个简单的 WPF 应用程序用于试验。

我在单独的 xaml 文件中定义了两个主题,更改 xaml 以指向它们效果很好。顺便说一句,在 xaml 中,我使用的是直接的 ResourceDictionary 元素,而不是 ResourceDictionary.MergedDictionaries 元素。

我想让用户选择要使用的主题,因此我在后面的代码中重置了源属性 - 但是虽然调试器告诉我我已经成功设置了该值,但应用程序的外观并没有改变。

那么,如何在运行时成功应用主题呢?

编辑:这就是我在 xaml 中声明我的“风格”的方式:

<Window x:Class="WpfUI.winMain">
  <Window.Resources>
    <ResourceDictionary Source="Themes\Blah.xaml"></ResourceDictionary>
  </Window.Resources>

  // The windows grid and other controls...

</Window>

I have a simple WPF application I'm using for experimenting.

I have two themes defined in seperate xaml files, changing the xaml to point to them worked fine. By the way, in the xaml I'm using a straight ResourceDictionary element, not a ResourceDictionary.MergedDictionaries one.

I want to let the user select which theme to use, so I'm reseting the source property in code behind - but whilst the debugger tells me I've successfully set the value the applications appearance doesn't change.

So, how do you successfully apply a theme at runtime?

EDIT: This is how I'm declaring my "style" in the xaml:

<Window x:Class="WpfUI.winMain">
  <Window.Resources>
    <ResourceDictionary Source="Themes\Blah.xaml"></ResourceDictionary>
  </Window.Resources>

  // The windows grid and other controls...

</Window>

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

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

发布评论

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

评论(1

耶耶耶 2024-09-21 03:36:06

简单的答案是,您需要清除应用程序合并的资源字典。这里有一些代码可以帮助您开始

ResourceDictionary dictionary = GetThemeResourceDictionary(yourTheme) 

if (dictionary != null)
{
     App.Current.Resources.MergedDictionaries.Clear();
     App.Current.Resources.MergedDictionaries.Add(dictionary);
}

public ResourceDictionary GetThemeResourceDictionary(string theme)
{
    if (theme != null)
    {
        Assembly assembly = Assembly.LoadFrom("WPF.Themes.dll");
        string packUri = String.Format(YourThemeFolder/{0}.xaml", theme);
        return Application.LoadComponent(new Uri(packUri, UriKind.Relative)) as ResourceDictionary;
    }
    return null;
}

如果您想要一个非常好的打包解决方案,我会推荐 WPF主题。它引入了一个 ThemeManager 类和一组确实令人难以置信的内置主题。如果您在安装或添加新主题时遇到任何困难,请与我联系:)

The simple answer is, you need to clear the applications merged resource dictionaries. Here is some code to get you started

ResourceDictionary dictionary = GetThemeResourceDictionary(yourTheme) 

if (dictionary != null)
{
     App.Current.Resources.MergedDictionaries.Clear();
     App.Current.Resources.MergedDictionaries.Add(dictionary);
}

public ResourceDictionary GetThemeResourceDictionary(string theme)
{
    if (theme != null)
    {
        Assembly assembly = Assembly.LoadFrom("WPF.Themes.dll");
        string packUri = String.Format(YourThemeFolder/{0}.xaml", theme);
        return Application.LoadComponent(new Uri(packUri, UriKind.Relative)) as ResourceDictionary;
    }
    return null;
}

If you want a really nice packaged solution, i would reccommend WPF themes. It introduces a ThemeManager class and a set of built in themes which are really incredible. If you have any difficulty installing it or adding a new theme, contact me :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文