如何让默认的 shell 弹出图标在 android 深色主题打开/关闭时做出反应

osh*_*nen 2 xaml maui .net-maui

我有一个使用 shell 弹出窗口的应用程序。当我添加弹出按钮时,它会自动创建一个图标,如下图所示:

深色模式下的默认弹出图标

但是,当我关闭 Android 深色主题时:

关闭深色主题

弹出图标仍然是白色的,因此很难看到:

在此输入图像描述

我正在使用 AppThemeBinding 根据用户选择的系统主题自动相应地设置应用程序主题,但如果用户从 Android 设置中关闭深色主题,我不知道如何将弹出图标更改为更深的颜色。

知道该怎么做吗?

AppShell.xaml 当前如下所示:

<?xml version="1.0" encoding="UTF-8" ?>
    
<Shell
    x:Class="MAUIApp1.AppShell"
    xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:local="clr-namespace:MAUIApp1"
    FlyoutBackgroundColor="{AppThemeBinding Light=#f2f2f2, Dark=#2a2a2a}">

    <Shell.Resources>
        <ResourceDictionary>
            ...
        </ResourceDictionary>
    </Shell.Resources>

    <Shell.FlyoutHeader>
        ...
    </Shell.FlyoutHeader>

    <Shell.ItemTemplate>
        <DataTemplate>
            ...
        </DataTemplate>
    </Shell.ItemTemplate>

    <FlyoutItem Title="Item1" Icon="item1.svg">
        <ShellContent ContentTemplate="{DataTemplate local:page1}" Route="page1"/>
    </FlyoutItem>

    <FlyoutItem Title="Item2" Icon="item2.svg">
        <ShellContent ContentTemplate="{DataTemplate local:page2}" Route="page2"/>
    </FlyoutItem>
    
    <FlyoutItem Title="Item3" Icon="item3.svg">
        <ShellContent ContentTemplate="{DataTemplate local:page3}" Route="page3"/>
    </FlyoutItem>

    <Shell.FlyoutFooter>
        ...
    </Shell.FlyoutFooter>
</Shell>
Run Code Online (Sandbox Code Playgroud)

Jul*_*ian 6

通常,您可以覆盖 Shell 的前景色来实现此目的:

<Shell
    x:Class="MAUIApp1.AppShell"
    xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:local="clr-namespace:MAUIApp1"
    FlyoutBackgroundColor="{AppThemeBinding Light=#f2f2f2, Dark=#2a2a2a}"
    Shell.ForegroundColor="{AppThemeBinding Light={StaticResource Black}, Dark={StaticResource White}}">
Run Code Online (Sandbox Code Playgroud)

或者,您可以通过编辑以下内容直接覆盖样式Resources\Styles\styles.xaml

<Style TargetType="Shell" ApplyToDerivedTypes="True">
    <Setter Property="Shell.BackgroundColor" Value="{AppThemeBinding Light={StaticResource Primary}, Dark={StaticResource Gray950}}" />
    <!--<Setter Property="Shell.ForegroundColor" Value="{OnPlatform WinUI={StaticResource Primary}, Default={StaticResource White}}" />-->
    <Setter Property="Shell.ForegroundColor" Value="{AppThemeBinding Light={StaticResource Black}, Dark={StaticResource White}}" />
</Style>
Run Code Online (Sandbox Code Playgroud)