如何在.Net MAUI 中为 ContentPage 标题添加样式?

Hoà*_*rần 3 .net winui-3 maui maui-windows

我正在使用 .NET MAUI 创建桌面应用程序。

在这里,我有一个ContentPage标题为My title 的标题。

<ContentPage
    x:Class="MAUI.Desktop.Pages.PlanningPage"
    xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    Title="My title">
....
Run Code Online (Sandbox Code Playgroud)

它的标题默认左对齐,你知道如何将其对齐到页面中心吗?如果可以的话,我也想给它添加更多的风格。 在此输入图像描述

编辑:建议的解决方案

  • 我忘记提及我的页面已被考虑NavigationPage。所以,这是我的解决方案:
<ContentPage
    x:Class="MAUI.Desktop.Pages.PlanningPage"
    xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
>
 <NavigationPage.TitleView>
        <Label
            x:Name="pageTitleView"
            HorizontalTextAlignment="Center"
            Text="My title"/>
</NavigationPage.TitleView>
......
Run Code Online (Sandbox Code Playgroud)

在代码隐藏xaml.cs文件中:

 protected override void OnSizeAllocated(double width, double height)
 {
     base.OnSizeAllocated(width, height);
     pageTitleView.WidthRequest = width;
 }
Run Code Online (Sandbox Code Playgroud)

mm8*_*mm8 7

您可以定义自己的TitleView

<ContentPage                          x:Class="MAUI.Desktop.Pages.PlanningPage"
    xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    Title="Planification">
    <Shell.TitleView>
        <Grid>
            <Label Text="My Title" HorizontalOptions="Center" VerticalOptions="Center" />
        </Grid>
    </Shell.TitleView>
    ...
Run Code Online (Sandbox Code Playgroud)