輕量級主題
DevExpress輕量級主題在視覺上復制常規主題,但提供更快的啟動時間和消耗更少的內存。
您可以在機器上比較常規和輕量級主題的性能,再運行我們在以下存儲庫中提供的特別設計的應用程序:
使用輕量級主題
將DevExpress.Wpf.ThemesLW NuGet包添加到您的項目中,或者引用DevExpress.Xpf.ThemesLW.v23.1程序集。
在應用程序構造函數中設置屬性為true。
在應用程序啟動時設置屬性為主題名稱,類包含可用的輕量級主題。
C#:
using DevExpress.Xpf.Core; // ... public partial class App : Application { static App() { CompatibilitySettings.UseLightweightThemes = true; } protected override void OnStartup(StartupEventArgs e) { ApplicationThemeHelper.ApplicationThemeName = LightweightTheme.Win10Dark.Name; base.OnStartup(e); } }
VB.NET:
Imports DevExpress.Xpf.Core ' ... Public Partial Class App Inherits Application Private Shared Sub New() CompatibilitySettings.UseLightweightThemes = True End Sub Protected Overrides Sub OnStartup(ByVal e As StartupEventArgs) ApplicationThemeHelper.ApplicationThemeName = LightweightTheme.Win10Dark.Name MyBase.OnStartup(e) End Sub End Class
輕量級的主題改變應用程序中所有控件的外觀,如果標準控件應該保持其原始外觀,則將附加的 屬性設置為false。
主題列表
Windows 10主題
Win10Dark:

Win10Light:

- Win10Dark
- Win10Light
- Win10System[1](讀取Windows應用模式)
- Win10SystemColors[1](讀取Windows應用程序模式和強調色)
Office 2019主題
Office2019Black:

Office2019Colorful (Default Theme):

Office2019HighContrast:

- Office2019Black
- Office2019Colorful(默認主題)
- Office2019HighContrast
- Office2019System[1](讀取Windows應用模式)
- 還包括一組預定義的黑色和彩色主題調色板。
Visual Studio 2019主題
VS2019Blue:

VS2019Dark:

VS2019Light:

- VS2019Blue
- VS2019Dark
- VS2019Light
- VS2019System[1](讀取Windows應用程序模式)
- 還包括一組預定義的藍色、深色和淺色主題的調色板。
調色板
調色板允許將顏色(例如,公司顏色)集成到應用程序中,并自定義主題中使用的顏色,您可以創建自定義調色板或使用。
Palette是一個的命名顏色,每個條目包括一個ColorName和一個Color值,您可以使用ColorName為任意數量的UI元素分配相應的顏色。
預定義的調色板
類包含預定義的調色板主題和經典主題,下面的代碼示例應用VS2019Dark主題與DeepSea調色板:
C#:
ApplicationThemeHelper.ApplicationThemeName = LightweightTheme.VS2019DarkDeepSea.Name;
VB.NET:
ApplicationThemeHelper.ApplicationThemeName = LightweightTheme.VS2019DarkDeepSea.Name
自定義調色板
您可以使用自定義調色板顏色來創建一個新的輕量級主題:
- 用調色板的顏色名稱和它的新顏色創建一個Dictionary。
要找到所需的顏色名稱,請查看以下文件夾中的輕量級主題資源:C:\Program Files\DevExpress 23.1\Components\Sources\XPF\DevExpress.Xpf.Themes\ThemesLW\Common。
- 使用方法創建一個新主題。
- 在中注冊創建的主題。
- 將此主題應用到應用程序中。
C#:
var customPalette = new Dictionary<string, Color> { {"Foreground", (Color)ColorConverter.ConvertFromString("#FFFF7200")}, {"SelectionBackground", Colors.Orange} }; var customTheme = LightweightTheme.OverridePalette(LightweightTheme.Win10Dark, "CustomTheme", "Custom Theme", customPalette); LightweightThemeManager.RegisterTheme(customTheme); ApplicationThemeHelper.ApplicationThemeName = customTheme.Name;
VB.NET:
Dim customPalette = New Dictionary(Of String, Color) From { {"Foreground", CType(ColorConverter.ConvertFromString("#FFFF7200"), Color)}, {"SelectionBackground", Colors.Orange} } Dim customTheme = LightweightTheme.OverridePalette(LightweightTheme.Win10Dark, "CustomTheme", "Custom Theme", customPalette) LightweightThemeManager.RegisterTheme(customTheme) ApplicationThemeHelper.ApplicationThemeName = customTheme.Name
修改輕量級主題資源
提示:我們不建議您修改可通過控件的內置API實現的任務的輕量級主題資源,輕量級主題資源和鍵可以在將來更改,因此更新到較新的DevExpress版本可能會更加復雜。
您可以自定義可視元素的主題資源(筆刷、厚度、顏色、樣式、模板等),如下面的主題所述:。
輕量級主題被設計為使用一組特定的主題鍵,每個DevExpress WPF程序集都包含LWKeyExtension類,您可以使用它來修改在此控件庫中定義的輕量級主題資源:
<Window ... xmlns:dxrt="http://schemas.devexpress.com/winfx/2008/xaml/ribbon/themekeys" xmlns:dxgt="http://schemas.devexpress.com/winfx/2008/xaml/grid/themekeys"> <Window.Resources> <!-- Overrides the 'GridColumnHeader.Background' theme resource in all themes: --> <SolidColorBrush x:Key="{dxgt:LWKey GridColumnHeader.Background}" Color="Red"/> <!-- Overrides the 'Backstage.Foreground' theme resource to 'Red' in all themes except 'Office2019Colorful'. In this theme, the code sets the resource to 'Blue': --> <SolidColorBrush x:Key="{dxrt:LWKey Backstage.Foreground}" Color="Red"/> <SolidColorBrush x:Key="{dxrt:LWKey Backstage.Foreground, ThemeName='Office2019Colorful'}" Color="Blue"/> <!-- LWKeys are similar to regular keys in most cases: --> <!-- For example, the '{dxgt:LWKey GridColumnHeader.Background}' key is equal to --> <!-- '{dxgt:GridColumnHeaderThemeKey ResourceKey=Background}' --> </Window.Resources> </Window>
在輕量級主題中,您可以不受任何地使用DynamicResource和StaticResource標記擴展,例如以下代碼在應用程序中的任何位置都有效:
<StackPanel ... xmlns:dxrt="http://schemas.devexpress.com/winfx/2008/xaml/ribbon/themekeys" xmlns:dxgt="http://schemas.devexpress.com/winfx/2008/xaml/grid/themekeys"> <!-- The DynamicResource is updated each time the application theme is changed. --> <!-- The StaticResource is resolved only once at the application startup. --> <Button Background="{StaticResource {dxgt:LWKey GridControl.Foreground}}"/> <Button Background="{DynamicResource {dxrt:LWKey Backstage.Foreground}}"/> </StackPanel>
用于修改常規主題資源的主題鍵也可以用于輕量級主題,唯一的區別是輕量級主題中的常規主題鍵變得與主題無關(不管ThemeName屬性值如何):
<Window ... xmlns:dxrt="http://schemas.devexpress.com/winfx/2008/xaml/ribbon/themekeys"> <Window.Resources> <!-- The following code does not work because the ThemeName property (defined in regular theme keys) is omitted in lightweight themes: --> <SolidColorBrush x:Key="{dxrt:BackstageThemeKey ResourceKey=Foreground, ThemeName=Office2019Colorful}" Color="Red"/> <SolidColorBrush x:Key="{dxrt:BackstageThemeKey ResourceKey=Foreground, ThemeName=Office2019Black}" Color="Blue"/> </Window.Resources> </Window>
使用說明
- 不支持。
- 輕量級主題沒有觸摸版本。
- 不能為輕量級主題。
- 在設計時不應用輕量級主題。
- 不能將輕量級主題應用于單個控件,只能應用于整個應用程序。