Home > Software design >  How to add style for Title of ContentPage in .Net MAUI?
How to add style for Title of ContentPage in .Net MAUI?

Time:12-03

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;
 }

CodePudding user response:

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>
    ...
  • Related