翻譯|使用教程|編輯:龔雪|2021-08-04 10:23:19.930|閱讀 283 次
概述:本教程將為大家介紹如何使用主題調(diào)色板,來(lái)優(yōu)化WPF應(yīng)用程序的主題,歡迎下載最新版體驗(yàn)~
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
DevExpress WPF控件調(diào)色板允許您將顏色(例如公司顏色)集成到WPF應(yīng)用程序中并自定義主題中使用的顏色,在此實(shí)例中您可以創(chuàng)建自定義調(diào)色板或使用預(yù)定義調(diào)色板。
Palette是命名顏色的列表,每種顏色都有一個(gè)ColorName值和一個(gè)Color值,您可以使用ColorName將顏色分配給任意數(shù)量的UI元素:
注意:您可以使用 WPF 主題設(shè)計(jì)器來(lái)編輯調(diào)色板顏色或?qū)⑵浣壎ǖ?UI 元素。
以下 DevExpress WPF 主題包含的調(diào)色板:
包括以下預(yù)定義的調(diào)色板:
注意:當(dāng)您切換主題時(shí),應(yīng)用程序不會(huì)卸載加載的主題程序集。
1. 在您的項(xiàng)目中引用 NuGet 包。
2. 調(diào)用 方法來(lái)啟用預(yù)定義的調(diào)色板。
3. 將 屬性設(shè)置為所需的預(yù)定義調(diào)色板名稱和基本主題名稱組合。
C#
Theme.RegisterPredefinedPaletteThemes(); ApplicationThemeHelper.ApplicationThemeName = PredefinedThemePalettes.RedWine.Name + Theme.Office2019White.Name;
VB.NET
Theme.RegisterPredefinedPaletteThemes() ApplicationThemeHelper.ApplicationThemeName = PredefinedThemePalettes.RedWine.Name + Theme.Office2019White.Name
TIP:您可以使用 屬性來(lái)緩存當(dāng)前調(diào)色板主題程序集,緩存減少了未來(lái)應(yīng)用程序運(yùn)行的加載時(shí)間。
上面的代碼示例為當(dāng)前主題啟用了所有可用的調(diào)色板,要啟用和應(yīng)用單個(gè)調(diào)色板:
1. 在您的項(xiàng)目中引用 NuGet 包。
2. 將調(diào)色板和基本主題傳遞給方法以創(chuàng)建新主題。
3. 將主題傳遞給 方法。
4. 將 屬性設(shè)置為主題名稱。
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
您可以在中顯示預(yù)定義的調(diào)色板,來(lái)允許用戶選擇調(diào)色板并將其應(yīng)用于當(dāng)前主題:
1. 參考 DevExpress.Mvvm.v21.1.dll 程序集。
2. 在應(yīng)用程序啟動(dòng)時(shí)調(diào)用 方法以啟用這些調(diào)色板:
C#
public partial class App : Application { protected override void OnStartup(StartupEventArgs e) { Theme.RegisterPredefinedPaletteThemes(); base.OnStartup(e); } }
VB.NET
Public Partial Class App Inherits Application Protected Overrides Sub OnStartup(ByVal e As StartupEventArgs) Theme.RegisterPredefinedPaletteThemes() MyBase.OnStartup(e) End Sub End Class
3. 將 RibbonGalleryItemThemePaletteSelectorBehavior 附加到 RibbonGalleryBarItem:
XAML
<dxr:RibbonGalleryBarItem ... > <dxmvvm:Interaction.Behaviors> <dxr:RibbonGalleryItemThemePaletteSelectorBehavior /> </dxmvvm:Interaction.Behaviors> </dxr:RibbonGalleryBarItem>
您可以使用帶有Windows 10 Light主題的Win10 Palette預(yù)定義調(diào)色板來(lái)獲取 Windows 10 強(qiáng)調(diào)色,并在您的應(yīng)用程序中使用該顏色。
以下代碼示例使用 Windows 10 強(qiáng)調(diào)色創(chuàng)建新的 Win10Light 主題,并在應(yīng)用程序啟動(dòng)時(shí)應(yīng)用該主題:
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.Name MyBase.OnStartup(e) End Sub
注意:Win10 Palette僅適用于 Windows 10 操作系統(tǒng)版本,Win10 Palette 中沒(méi)有找到Windows 強(qiáng)調(diào)色,應(yīng)用程序強(qiáng)調(diào)色設(shè)置為#FF0078D7。
在代碼中編輯調(diào)色板
注意:當(dāng)您切換主題時(shí),應(yīng)用程序不會(huì)卸載加載的主題程序集。
將自定義調(diào)色板應(yīng)用于應(yīng)用程序:
1. 在您的項(xiàng)目中引用 NuGet 包。
2. 創(chuàng)建一個(gè)新的 實(shí)例:
C#
var custompalette = new ThemePalette("CustomPalette");
VB.NET
Dim custompalette = New ThemePalette("CustomPalette")
...或基于預(yù)定義的調(diào)色板創(chuàng)建一個(gè)新的 ThemePalette 實(shí)例,在這種情況下,新調(diào)色板繼承了預(yù)定義調(diào)色板的顏色:
C#
var custompalette = new ThemePalette("CustomPalette", PredefinedThemePalettes.RedWine);
VB.NET
Dim custompalette = new ThemePalette("CustomPalette", PredefinedThemePalettes.RedWine)
3. 使用方法指定新顏色:
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)
將調(diào)色板和具有調(diào)色板支持的主題傳遞給 方法來(lái)創(chuàng)建新主題:
C#
var customtheme = Theme.CreateTheme(custompalette, Theme.Office2016ColorfulSE);
VB.NET
Dim customtheme = Theme.CreateTheme(custompalette, Theme.Office2016ColorfulSE)
將主題傳遞給方法并將設(shè)置為主題的名稱,來(lái)將主題應(yīng)用于應(yīng)用程序:
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
#在使用單個(gè)文件部署的 .NET 5 應(yīng)用程序中在運(yùn)行時(shí)更改主題
DevExpress WPF 主題程序集必須提取到磁盤,發(fā)布 .NET 5 應(yīng)用程序(PublishSingleFile 為 true)時(shí),將項(xiàng)目文件中的 IncludeAllContentForSelfExtract 選項(xiàng)設(shè)置為 true,這將允許用戶在運(yùn)行時(shí)應(yīng)用調(diào)色板。
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擁有120+個(gè)控件和庫(kù),將幫助您交付滿足甚至超出企業(yè)需求的高性能業(yè)務(wù)應(yīng)用程序。通過(guò)DevExpress WPF能創(chuàng)建有著強(qiáng)大互動(dòng)功能的XAML基礎(chǔ)應(yīng)用程序,這些應(yīng)用程序?qū)W⒂诋?dāng)代客戶的需求和構(gòu)建未來(lái)新一代支持觸摸的解決方案。 無(wú)論是Office辦公軟件的衍伸產(chǎn)品,還是以數(shù)據(jù)為中心的商業(yè)智能產(chǎn)品,都能通過(guò)DevExpress WPF控件來(lái)實(shí)現(xiàn)。
DevExpress技術(shù)交流群4:715863792 歡迎一起進(jìn)群討論
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@fc6vip.cn
文章轉(zhuǎn)載自:慧都網(wǎng)