MAUI新生4.6-主题设置LightTheme&DarkTheme

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

  • 定义每个应用主题的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());
        }
    }
}

 

posted @ 2022-12-22 22:45  functionMC  阅读(998)  评论(0编辑  收藏  举报