MAUI自定义导航栏的核心是Shell.TitleView,它允许用任意XAML内容完全替换默认标题栏;需确保页面为Shell子页面,TitleView必须是ContentPage直接子元素,支持绑定与高度控制。

MAUI 中实现自定义导航栏,最标准、推荐的方式就是用 Shell.TitleView。它允许你完全替换默认的标题栏区域(包括标题文字、返回按钮等),用任意 XAML 内容(比如 StackLayout、Grid、Image、Button 等)来构建自己的导航栏外观和交互。
确保你的页面是 Shell 的子页面(即在 AppShell.xaml 中通过 FlyoutItem 或 TabBar 导航到该页),否则 TitleView 不生效。页面本身不需要继承 ShellContent,而是作为 ContentPage 放在 ShellContent.ContentTemplate 里。
<shellcontent contenttemplate="{DataTemplate local:MyPage}"></shellcontent>
Shell.TitleView 即可生效直接在 ContentPage 根元素内添加 Shell.TitleView,里面放你想要的 UI:
<ContentPage ...>
<Shell.TitleView>
<Grid BackgroundColor="LightBlue" Padding="10">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Button Text="←" Command="{Binding GoBackCommand}" Grid.Column="0"/>
<Label Text="我的页面" HorizontalOptions="Center" Grid.Column="1"/>
<Button Text="⋯" Grid.Column="2"/>
</Grid>
</Shell.TitleView>
<p><!-- 页面主体内容 -->
<VerticalStackLayout Padding="20">
<Label Text="页面内容"/>
</VerticalStackLayout>
</ContentPage>注意:Shell.TitleView 必须是 ContentPage 的直接子元素,不能嵌套在其他布局里;它不会影响页面主体内容的布局位置,系统会自动为 TitleView 预留顶部空间。
HeightRequest,但建议优先适配平台习惯Shell.GoToAsync("..") 实现返回Shell.NavBarIsVisible="False" 并自行处理全屏导航栏(更复杂,非必需)<label text="{Binding PageTitle}"></label>,配合 ViewModel 更新标题基本上就这些。TitleView 灵活又轻量,不用重写 Shell 模板,适合大多数定制需求。如果需要更底层控制(比如全局统一导航栏样式或动画),才考虑自定义 ShellRenderer 或用 NavigationPage 替代 Shell —— 但那是另一条路了。
以上就是MAUI怎么实现自定义导航栏 MAUI Shell TitleView用法的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号