3

I'm working with .NET MAUI to create a desktop app.

Here, I have a ContentPage with the title 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">
....

Its title is aligned left by default, do you know how to align it to the center of the page? If possible, I also want to add more style to it too. enter image description here

Edit: suggested solution

  • I forget to mention that my page is considered NavigationPage. So, here is my solution:
<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>
......

In code-behind xaml.cs file:

 protected override void OnSizeAllocated(double width, double height)
 {
     base.OnSizeAllocated(width, height);
     pageTitleView.WidthRequest = width;
 }
Hoàng Trần
  • 549
  • 6
  • 14

1 Answers1

7

You could define your own 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>
    ...
mm8
  • 163,881
  • 10
  • 57
  • 88
  • 1
    Your answer works well in case I use `Shell App`. But actually, my `ContentPage` is set up as a `NavigationPage`, so I have to modify the solution a little bit. I accept the answer anyway. Thanks for your time – Hoàng Trần Dec 03 '22 at 12:29