.NET MAUI:如何修改默认项目标题栏字体或移除标题?
.NET MAUI 标题栏调整方案(移除/缩小/自定义样式)
一、完全移除标题栏
直接通过Shell的全局属性即可实现,无需修改ShellContent:
<Shell x:Class="MauiApp2.AppShell" xmlns="http://schemas.microsoft.com/dotnet/2021/maui" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:MauiApp2" Shell.FlyoutBehavior="Disabled" Shell.NavBarIsVisible="False"> <!-- 添加此属性隐藏标题栏 --> <ShellContent Title="Home" ContentTemplate="{DataTemplate local:MainPage}" Route="MainPage" /> </Shell>
如果仅需在单个页面隐藏标题栏,可在对应页面的ContentPage标签中添加NavigationPage.HasNavigationBar="False"。
二、自定义标题栏(缩小尺寸/调整字体/边距)
ShellContent本身不支持FontSize、Margin等属性,需要通过Shell.TitleView替换默认标题栏,实现完全自定义:
<Shell x:Class="MauiApp2.AppShell" xmlns="http://schemas.microsoft.com/dotnet/2021/maui" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:MauiApp2" Shell.FlyoutBehavior="Disabled"> <!-- 自定义标题栏,按需调整参数 --> <Shell.TitleView> <Grid HeightRequest="32" BackgroundColor="{DynamicResource Primary}"> <!-- 设置标题栏高度 --> <Label Text="Home" FontSize="14" <!-- 调整字体大小 --> Margin="12,4" <!-- 控制外边距 --> Padding="0" <!-- 清除默认内边距 --> VerticalOptions="Center" TextColor="White"/> </Grid> </Shell.TitleView> <ShellContent ContentTemplate="{DataTemplate local:MainPage}" Route="MainPage" /> <!-- 可移除Title属性,避免重复 --> </Shell>
三、解决你遇到的其他问题
- ShellContent设置IsVisible="False"崩溃:ShellContent是Shell的核心导航节点,隐藏后Shell无法找到默认启动页面,导致空引用异常。隐藏标题栏请使用
Shell.NavBarIsVisible="False",而非隐藏ShellContent。 - 后台修改Title不生效:若使用默认标题栏,需确保在Shell初始化完成后修改ShellContent的Title;若使用自定义
TitleView,则直接修改View中控件的Text属性即可:
public AppShell() { InitializeComponent(); // 修改默认标题栏的Title var mainContent = Items.FirstOrDefault(i => i.Route == "MainPage") as ShellContent; mainContent?.SetValue(ShellContent.TitleProperty, "新标题"); // 若用自定义TitleView,直接修改Label的Text var titleLabel = (Label)TitleView.FindByName("TitleLabel"); titleLabel.Text = "新标题"; }
内容的提问来源于stack exchange,提问作者AndreasW
相关产品推荐
相关产品推荐

