通过主题设置,可以在运行时更改应用的主题外观,比如切换亮色主题和暗黑主题。主题设置并没有新的知识点,基本的原理如下:

  • 定义每个应用主题的ResourceDictionary,每个ResourceDictionary有相同的Key,但值不同。
  • 在根页面App.xaml的资源字典中,引用默认的ResourceDictionary。
  • 使用方式①:直接在页面中,通过DynamicResource扩展标记,引用ResourceDictionary的Key值。
  • 使用方式②:在App.xaml中,定义应用级别的Style,样式值使用DynamicResource扩展标记,引用ResourceDictionary的Key值。页面中,则使用StaticResource扩展标记引用Style。
  • 如需在运行时更改主题,通过后台代码更换ResourceDictionary即可。

一、在Themes文件夹下,创建MAUI的资源字典文件LightTheme.xaml和DarkTheme.xaml

 二、在根页面App.xaml的资源字典中,引用默认的ResourceDictionary

<Application
......>
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resources/Styles/Colors.xaml" />
<ResourceDictionary Source="Resources/Styles/Styles.xaml" />
<!--引用默认主题资源字典LightTheme.xaml-->
<ResourceDictionary Source="Themes/LightTheme.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>

三、在App.xaml中,定义应用级别的Style(非必要)

<Application
......>
<Application.Resources>
<ResourceDictionary>
......
</ResourceDictionary>
<!--定义Style,TargetType为Label-->
<Style x:Key="PrimaryLabelStyle" TargetType="Label">
<Setter Property="TextColor" Value="{DynamicResource PrimaryTextColor}" />
<Setter Property="FontSize" Value="25" />
</Style>
<Style x:Key="SecondLabelStyle" TargetType="Label">
<Setter Property="TextColor" Value="{DynamicResource SecondaryTextColor}" />
<Setter Property="FontSize" Value="30" />
</Style>
</Application.Resources>
</Application>

四、在页面中通过DynamicResource扩展标记引用ResourceDictionary的Key值,或通过StaticResource和Style间接引用

<ContentPage
......
BackgroundColor="{DynamicResource PageBackgroundColor}">
<StackLayout BackgroundColor="{DynamicResource PrimaryColor}">
<Label Text="直接绑定ResourceDictionary的Key值①" TextColor="{DynamicResource PrimaryTextColor}"/>
<Label Text="直接绑定ResourceDictionary的Key值②" TextColor="{DynamicResource SecondaryTextColor}"/>
<Label Text="通过Style间接绑定ResourceDictionary①" Style="{StaticResource PrimaryLabelStyle}"/>
<Label Text="通过Style间接绑定ResourceDictionary②" Style="{StaticResource PrimaryLabelStyle}"/>
<Button Text="切换主题" Clicked="Button_Clicked"/>
</StackLayout>
</ContentPage>

五、通过后台代码切换主题,本案例使用Button的点击事件

public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
} private void Button_Clicked(object sender, EventArgs e)
{
//获取当前资源字典
ICollection<ResourceDictionary> mergedDictionaries = Application.Current.Resources.MergedDictionaries;
//先将当前资源字典清空,再添回暗黑主题DarkTheme
if (mergedDictionaries != null)
{
mergedDictionaries.Clear();
mergedDictionaries.Add(new DarkTheme());
}
}
}

最新文章

  1. [Linux] - centos使用mount + nfs 远程共享存储
  2. P1905生活大爆炸版 石头剪刀布
  3. 可输出sql的PrepareStatement封装
  4. QQ空间个人中心的广告
  5. 自己通过Cygwin编译的windows下的redis3.2.6
  6. Objective-C的基础语法总结
  7. Oracle笔记 六、PL/SQL简单语句块、变量定义
  8. asp.net各种获取客户端ip方法
  9. iOS--导航栏样式
  10. windows下安装MySQLdb模块
  11. hibernate 批量处理数据
  12. SCM白色幼儿系列(十二) Proteus仿真软件简介
  13. android下载管理、理财、浏览器、商品筛选、录音源码等
  14. APIO2010特别行动队
  15. 2.4、Android Studio使用主题编辑器设计主题
  16. VS Code 1.28版本设置中文界面的方法
  17. Apache Flume 1.7.0 自定义输入输出
  18. 多线程内存问题分析之mprotect方法【转】
  19. 【C++】C++中类的基本使用
  20. 『Numpy』np.meshgrid

热门文章

  1. PHP全栈开发(四): HTML 学习(2. div 布局)
  2. Java Style的C++容器流式处理类
  3. SpringBoot (四) - 整合Mybatis,逆向工程,JPA
  4. 删除数组里含有a的元素,并且将null值放在后面
  5. java中获取当前执行线程的名称
  6. 齐博x2模型里边钩子的创建与使用
  7. C#中进行数值的比较
  8. ATT&amp;CK框架整理(中英文整理)
  9. Python基础部分:9、数据的类型和内置方法
  10. 【Azure 事件中心】Event Hub 无法连接,出现 Did not observe any item or terminal signal within 60000ms in &#39;flatMapMany&#39; 的错误消息