本教程将为大家介绍如何使用主题调色板,来优化WPF应用程序的主题,欢迎下载最新版体验~

Palette是命名颜色的列表,每种颜色都有一个ColorName值和一个Color值,您可以使用ColorName将颜色分配给任意数量的UI元素:


以下 DevExpress WPF 主题包含的调色板:

预定义的调色板
Palette Themes包括以下预定义的调色板:

在代码中应用调色板
注意:当您切换主题时,应用程序不会卸载加载的主题程序集。
1. 在您的项目中引用 Mono.cecil NuGet 包。
2. 调用 Theme.RegisterPredefinedPaletteThemes 方法来启用预定义的调色板。
3. 将 ApplicationThemeHelper.ApplicationThemeName 属性设置为所需的预定义调色板名称和基本主题名称组合。
C#
Theme.RegisterPredefinedPaletteThemes();ApplicationThemeHelper.ApplicationThemeName = PredefinedThemePalettes.RedWine.Name + Theme.Office2019White.Name;
VB.NET
Theme.RegisterPredefinedPaletteThemes()ApplicationThemeHelper.ApplicationThemeName = PredefinedThemePalettes.RedWine.Name + Theme.Office2019White.Name
TIP:您可以使用 Theme.CachePaletteThemes 属性来缓存当前调色板主题程序集,缓存减少了未来应用程序运行的加载时间。
上面的代码示例为当前主题启用了所有可用的调色板,要启用和应用单个调色板:
1. 在您的项目中引用 Mono.cecil NuGet 包。
2. 将调色板和基本主题传递给Theme.CreateTheme方法以创建新主题。
3. 将主题传递给 Theme.RegisterTheme方法。
4. 将 ApplicationThemeHelper.ApplicationThemeName 属性设置为主题名称。
C#
var palettetheme = Theme.CreateTheme(PredefinedThemePalettes.RedWine, Theme.Office2019White);Theme.RegisterTheme(palettetheme);ApplicationThemeHelper.ApplicationThemeName = palettetheme.Name;
VB.NET
Dim palettetheme = Theme.CreateTheme(PredefinedThemePalettes.RedWine, Theme.Office2019White)Theme.RegisterTheme(palettetheme)ApplicationThemeHelper.ApplicationThemeName = palettetheme.Name
在Ribbon库中显示调色板
您可以在Ribbon Gallery中显示预定义的调色板,来允许用户选择调色板并将其应用于当前主题:

1. 参考 DevExpress.Mvvm.v21.1.dll 程序集。
2. 在应用程序启动时调用Theme.RegisterPredefinedPaletteThemes 方法以启用这些调色板:
C#
public partial class App : Application {protected override void OnStartup(StartupEventArgs e) {Theme.RegisterPredefinedPaletteThemes();base.OnStartup(e);}}
VB.NET
Public Partial Class AppInherits ApplicationProtected Overrides Sub OnStartup(ByVal e As StartupEventArgs)Theme.RegisterPredefinedPaletteThemes()MyBase.OnStartup(e)End SubEnd Class
3. 将 RibbonGalleryItemThemePaletteSelectorBehavior 附加到 RibbonGalleryBarItem:
XAML
<dxr:RibbonGalleryBarItem ... ><dxmvvm:Interaction.Behaviors><dxr:RibbonGalleryItemThemePaletteSelectorBehavior /></dxmvvm:Interaction.Behaviors></dxr:RibbonGalleryBarItem>
使用 Windows 10 强调色
您可以使用带有Windows 10 Light主题的Win10 Palette预定义调色板来获取 Windows 10 强调色,并在您的应用程序中使用该颜色。

以下代码示例使用 Windows 10 强调色创建新的 Win10Light 主题,并在应用程序启动时应用该主题:
C#
protected override void OnStartup(StartupEventArgs e){var accentpalette = new Win10Palette();var customtheme = Theme.CreateTheme(accentpalette, Theme.Win10Light);Theme.RegisterTheme(customtheme);ApplicationThemeHelper.ApplicationThemeName = customtheme.Name;base.OnStartup(e);}
VB.NET
Protected Overrides Sub OnStartup(ByVal e As StartupEventArgs)Dim accentpalette = New Win10Palette()Dim customtheme = Theme.CreateTheme(accentpalette, Theme.Win10Light)Theme.RegisterTheme(customtheme)ApplicationThemeHelper.ApplicationThemeName = customtheme.NameMyBase.OnStartup(e)End Sub
注意:Win10 Palette仅适用于 Windows 10 操作系统版本,Win10 Palette 中没有找到Windows 强调色,应用程序强调色设置为#FF0078D7。
自定义调色板
注意:当您切换主题时,应用程序不会卸载加载的主题程序集。
将自定义调色板应用于应用程序:
1. 在您的项目中引用 Mono.cecil NuGet 包。
2. 创建一个新的 ThemePalette 实例:
C#
var custompalette = new ThemePalette("CustomPalette");
VB.NET
Dim custompalette = New ThemePalette("CustomPalette")
…或基于预定义的调色板创建一个新的 ThemePalette 实例,在这种情况下,新调色板继承了预定义调色板的颜色:
C#
var custompalette = new ThemePalette("CustomPalette", PredefinedThemePalettes.RedWine);
VB.NET
Dim custompalette = new ThemePalette("CustomPalette", PredefinedThemePalettes.RedWine)
3. 使用ThemePalette.SetColor方法指定新颜色:
C#
custompalette.SetColor("Foreground", (Color)ColorConverter.ConvertFromString("#FFFF7200"));custompalette.SetColor("Backstage.Focused", Colors.White);
VB.NET
custompalette.SetColor("Foreground", ColorConverter.ConvertFromString("#FF015C9F"))custompalette.SetColor("Foreground", Colors.White)
将调色板和具有调色板支持的主题传递给Theme.CreateTheme 方法来创建新主题:
C#
var customtheme = Theme.CreateTheme(custompalette, Theme.Office2016ColorfulSE);
VB.NET
Dim customtheme = Theme.CreateTheme(custompalette, Theme.Office2016ColorfulSE)
将主题传递给Theme.RegisterTheme方法并将ApplicationThemeHelper.ApplicationThemeName设置为主题的名称,来将主题应用于应用程序:
C#
var custompalette = new ThemePalette("CustomPalette");custompalette.SetColor("Foreground", (Color)ColorConverter.ConvertFromString("#FFFF7200"));custompalette.SetColor("Backstage.Focused", Colors.White);var customtheme = Theme.CreateTheme(custompalette, Theme.Office2016ColorfulSE);Theme.RegisterTheme(customtheme);ApplicationThemeHelper.ApplicationThemeName = customtheme.Name;
VB.NET
Dim custompalette = New ThemePalette("CustomPalette")custompalette.SetColor("Foreground", ColorConverter.ConvertFromString("#FF015C9F"))custompalette.SetColor("Backstage.Focused", Colors.White)Dim customtheme = Theme.CreateTheme(custompalette, Theme.Office2016ColorfulSE)Theme.RegisterTheme(customtheme)ApplicationThemeHelper.ApplicationThemeName = customtheme.Name
局限性
#在使用单个文件部署的 .NET 5 应用程序中在运行时更改主题
DevExpress WPF 主题程序集必须提取到磁盘,发布 .NET 5 应用程序(PublishSingleFile 为 true)时,将项目文件中的 IncludeAllContentForSelfExtract 选项设置为 true,这将允许用户在运行时应用调色板。
XAML
<PropertyGroup><OutputType>WinExe</OutputType><TargetFramework>net5.0-windows</TargetFramework><UseWPF>true</UseWPF><Nullable>enable</Nullable><PublishSingleFile>true</PublishSingleFile><IncludeAllContentForSelfExtract>true</IncludeAllContentForSelfExtract></PropertyGroup>
DevExpress WPF | 下载试用
DevExpress WPF拥有120+个控件和库,将帮助您交付满足甚至超出企业需求的高性能业务应用程序。通过DevExpress WPF能创建有着强大互动功能的XAML基础应用程序,这些应用程序专注于当代客户的需求和构建未来新一代支持触摸的解决方案。 无论是Office办公软件的衍伸产品,还是以数据为中心的商业智能产品,都能通过DevExpress WPF控件来实现。
DevExpress技术交流群4:715863792 欢迎一起进群讨论

标签:
声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!