.NET MAUI标题栏颜色异常Bug是否存在可行解决方案?
.NET MAUI标题栏异常变色问题排查
问题现象
搭建极简.NET MAUI项目,包含3个页面,通过Shell导航实现两种跳转路径:
- 页面A通过绝对路径
//SecondPage/ModalPage导航至页面C - 页面B通过相对路径
ModalPage将页面C推入导航栈
两种路径从C返回后均回到页面B,但出现不一致现象:
- B→C→返回B:页面B的标题栏颜色保持正常
- A→B/C→返回B:页面B的标题栏意外变为深灰色,离开B页面再返回后颜色恢复正常
测试环境
Pixel 5 Android 13 API 33模拟器,使用设备物理返回键或页面内返回按钮触发问题,现象一致。
更新说明
该疑似Bug已提交至.NET MAUI官方GitHub问题专区。
问题演示

完整项目代码
AppShell代码
<Shell x:Class="TitleBarTest.AppShell" xmlns="http://schemas.microsoft.com/dotnet/2021/maui" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:TitleBarTest" Shell.FlyoutBehavior="Flyout"> <FlyoutItem Title="Title Bar Test"> <ShellContent ContentTemplate="{DataTemplate local:MainPage}" Shell.FlyoutItemIsVisible="True" Shell.TabBarIsVisible="True" Title="Page A" Route="MainPage"/> <ShellContent ContentTemplate="{DataTemplate local:SecondPage}" Shell.FlyoutItemIsVisible="True" Shell.TabBarIsVisible="True" Title="Page B" Route="SecondPage"/> </FlyoutItem> <ShellContent ContentTemplate="{DataTemplate local:ModalPage}" Shell.FlyoutItemIsVisible="False" Shell.TabBarIsVisible="False" Title="Page C" Route="ModalPage"/> </Shell>
Page A(MainPage)
XAML布局
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:TitleBarTest" x:DataType="local:MainPageViewModel" x:Class="TitleBarTest.MainPage" Title="Page A"> <VerticalStackLayout> <Label Text="List of A-items" VerticalOptions="Center" HorizontalOptions="Center" /> <Button Command="{Binding GoToModalViaSecondPageCommand}" x:Name="CounterBtn" Text="Go to C via B" HorizontalOptions="Center" /> </VerticalStackLayout> </ContentPage>
ViewModel逻辑
public class MainPageViewModel : BaseViewModel { public Command GoToModalViaSecondPageCommand { get; private set; } public MainPageViewModel() { GoToModalViaSecondPageCommand = new Command(GoToModalViaSecondPage); } public async void GoToModalViaSecondPage() { await Shell.Current.GoToAsync($"//{nameof(SecondPage)}/{nameof(ModalPage)}"); } }
Page B(SecondPage)
XAML布局
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:TitleBarTest" x:DataType="local:SecondPageViewModel" x:Class="TitleBarTest.SecondPage" Title="Page B"> <VerticalStackLayout> <Label Text="List of B-items" VerticalOptions="Center" HorizontalOptions="Center" /> <Button Command="{Binding GoToModalDirectlyCommand}" x:Name="CounterBtn" Text="Click me" SemanticProperties.Hint="Counts the number of times you click" HorizontalOptions="Center" /> </VerticalStackLayout> </ContentPage>
ViewModel逻辑
public class SecondPageViewModel : BaseViewModel { public Command GoToModalDirectlyCommand { get; private set; } public SecondPageViewModel() { GoToModalDirectlyCommand = new Command(GoToModalDirectly); } public async void GoToModalDirectly() { await Shell.Current.GoToAsync($"{nameof(ModalPage)}"); } }
Page C(ModalPage)
XAML布局
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:TitleBarTest" x:DataType="local:ModalPageViewModel" x:Class="TitleBarTest.ModalPage" Shell.PresentationMode="ModalAnimated" BackgroundColor="#AA000000" Title="Page C"> <StackLayout VerticalOptions="End"> <Frame BackgroundColor="White" CornerRadius="24" HeightRequest="256" Margin="0,0,0,-24"> <VerticalStackLayout> <Label Text="Create new B-item" VerticalOptions="Center" HorizontalOptions="Center" /> </VerticalStackLayout> </Frame> </StackLayout> </ContentPage>
ViewModel逻辑
public class ModalPageViewModel : BaseViewModel { public Command BackToPageBCommand { get; private set; } public ModalPageViewModel() { BackToPageBCommand = new Command(BackToPageB); } public async void BackToPageB() { await Shell.Current.GoToAsync($"//{nameof(SecondPage)}"); } }
Styles.xaml默认配置补充
针对评论反馈:Shell.BackgroundColor默认在Styles.xaml中定义,移除该设置后所有标题栏会变为灰色,存在默认回退颜色,默认代码如下:
<Style TargetType="Shell" ApplyToDerivedTypes="True"> <Setter Property="Shell.BackgroundColor" Value="{AppThemeBinding Light={StaticResource Primary}, Dark={StaticResource Gray950}}" /> ... ... ... </Style>
内容的提问来源于stack exchange,提问作者user7862339
相关产品推荐
相关产品推荐

