打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
C# WPF UI框架MahApps切换主题

概述

      本指南将向您介绍MahApps.Metro如何切换主题,所有的MahApps.Metro的主题都包含在单独的资源字典中。

如何使用主题

您可以在以下可用配色方案中进行选择:

"Red", "Green", "Blue", "Purple", "Orange", "Lime", "Emerald", "Teal", "Cyan", "Cobalt", "Indigo", "Violet", "Pink", "Magenta", "Crimson", "Amber", "Yellow", "Brown", "Olive", "Steel", "Mauve", "Taupe", "Sienna"

这些基本主题:

"Light", "Dark"

通过App.xaml使用主题:

最快的方法是在App.xaml中指定主题资源

<Application x:Class="SampleApp"             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" />        <!-- Theme setting -->        <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Themes/Light.Blue.xaml" />      </ResourceDictionary.MergedDictionaries>    </ResourceDictionary>  </Application.Resources></Application>

通过主题管理器使用主题:

MahApps.Metro有一个ThemeManager类,可以使用后台代码更改主题。可以在一行中完成,如下所示:

using ControlzEx.Theming;
public partial class SampleApp : Application{ protected override void OnStartup(StartupEventArgs e) { base.OnStartup(e);
// Set the application theme to Dark.Green ThemeManager.Current.ChangeTheme(this, "Dark.Green"); }}

在与应用程序主窗口不同的窗口上

和MahApps在一样,对于MetroWindow,你可以有不同的主题。主窗口或任何其他MetroWindows将在应用程序中保留指定的主题。

<Controls:MetroWindow.Resources>    <ResourceDictionary>        <ResourceDictionary.MergedDictionaries>            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Themes/Dark.Red.xaml" />        </ResourceDictionary.MergedDictionaries>    </ResourceDictionary></Controls:MetroWindow.Resources>

您也可以使用TheManager执行此操作,如下所示:

using ControlzEx.Theming;
public partial class MainWindow : MetroWindow{ public void MainWindow() { InitializeComponent();
// Set the window theme to Dark.Red ThemeManager.Current.ChangeTheme(this, "Dark.Red"); }}

创建自定义主题

MahApps的另一个不错的功能。Metro将使用自定义创建的主题或运行时创建的主题。

声明

翻译自:https://mahapps.com/docs/themes/usage#on-a-window-different-to-your-applications-main-window
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
wpf开源控件MahApps.Metro
一个Metro风格的开源项目 MahApps.Metro
Silverlight动态载入调用XAML资源
XAML实例教程系列
【WPF开源控件库】Material Design in XAML Toolkit
C# WPF遮罩对话框(Popup Message Overlay/ Dialog Host)
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服