【问题标题】:How to add .xaml to create a custom Theme on MahApps.Metro如何添加 .xaml 以在 MahApps.Metro 上创建自定义主题
【发布时间】:2026-02-09 23:50:02
【问题描述】:

我是 WPF 的新手,现在我正在尝试制作自定义主题,特别是深色模式(使用其他颜色,而不是默认的 BaseDark),所以我在这里阅读了文档:

https://mahapps.com/guides/styles.html#custom

但我不知道如何处理它。该代码是一个.xaml,对吗?像 App.xaml 中引用的 BaseDark.xaml

<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseDark.xaml" />

那么,如何在项目中实现它?我读到here 我必须“在 Styles/Accents 文件夹下创建 MyCustomAccent.xaml”,但我不知道该怎么做,而且帖子很旧

那么问题来了,如何将我的自定义 .xaml 主题添加到我的项目中?

我知道以后我必须使用 ThemeManager.ChangeAppTheme 在 BaseLight 和我的 CustomDark 之间切换,我已经制作了一个切换按钮,可以在 BaseLight 和 BaseDark 之间切换

【问题讨论】:

    标签: c# mahapps.metro


    【解决方案1】:

    此页面为您提供一步一步:https://mahapps.com/guides/quick-start.html#styling

    想法是您将样式字典添加到窗口,如下所示:

    <Application x:Class="WpfApplication.App"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 StartupUri="MainWindow.xaml">
      <Application.Resources>
        <ResourceDictionary>
          <ResourceDictionary.MergedDictionaries>
            <!-- MahApps.Metro resource dictionaries. Make sure that all file names are Case Sensitive! -->
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" />
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colors.xaml" />
            <!-- Accent and AppTheme setting -->
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Blue.xaml" />
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseLight.xaml" />
          </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
      </Application.Resources>
    </Application>
    

    【讨论】: