自定义标题视图
如果您使用默认的 MAUI 应用程序模板,那么您正在使用 Shell,它允许您设置自定义 TitleView:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:views="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
x:Class="MyApp.Views.MyPage"
Title="MyPage">
<Shell.TitleView>
<Grid>
<Label
Text="Hello"
HorizontalOptions="Start"
VerticalOptions="Center"
BackgroundColor="Orange"
TextColor="White"
FontSize="Title" />
</Grid>
</Shell.TitleView>
<!-- .. -->
</ContentPage>
这允许您完全自定义 TitleView、向其添加按钮、更改字体、大小和颜色等。
标题间距/ContentInsetStartWithNavigation
如果没有标题和菜单图标之间的间距,则无法更改映射器/处理程序或自定义渲染器。
您可以将以下内容附加到Toolbar
在你的代码中的某个地方,例如在你的MauiProgram.cs:
Microsoft.Maui.Handlers.ToolbarHandler.Mapper.AppendToMapping("CustomNavigationView", (handler, view) =>
{
#if ANDROID
handler.PlatformView.ContentInsetStartWithNavigation = 0;
#endif
});
变更前:
更改后:
汉堡菜单图标颜色
也可以更改汉堡菜单图标颜色,但这已在其他问题中涵盖:
如何让默认的 shell 弹出图标在 android 深色主题打开/关闭时做出反应
使用 shell 时如何更改毛伊岛汉堡吧的颜色
更改 .NET MAUI 应用程序中的汉堡菜单图标