如何更改导航标题视图的背景颜色 - Xamarin Forms?

问题描述

这是我第一次使用导航标题视图。我遇到的问题是 Android 中导航栏的背景与 iOS 中的不同。我想将背景的颜色设置为白色,这样对于 iOS 和 Android 来说都是一样的。任何建议如何做到这一点。我已经包含了下面的代码图片

XAML 代码

    <NavigationPage.TitleView>
    <Label           FontSize="25"
                     TextColor="Black"
                     VerticalOptions="CenterandExpand"
                     HorizontalOptions="CenterandExpand"
                     HorizontalTextAlignment="Center"
                     FontFamily="PatrickFont">
                <Label.FormattedText>
                    <FormattedString>
                        <Span Text="Number"/>
                        <Span Text="Generator" TextColor="Red"/>
                    </FormattedString>
                </Label.FormattedText>
            </Label>
</NavigationPage.TitleView>

CS 代码

private void InfoIconCommandExecute(object obj)
{
   App.Current.MainPage.Navigation.PushAsync(new InfoPage());
}

enter image description here

enter image description here

解决方法

选项 1. 在 app.xaml 文件中设置导航页面颜色。这将影响您应用中的所有页面。

...
<Application.Resources>
   <Style TargetType="NavigationPage">
      <Setter Property="BarBackgroundColor" Value="White"/>
      <Setter Property="BarTextColor" Value="OrangeRed" />
   </Style>
</Application.Resources>
...

选项 2. 在页面文件中设置要更改导航背景颜色。 YourPage.xaml.cs

...
public YourPage()
{
   InitializeComponent();
   ((NavigationPage)Application.Current.MainPage).BarBackgroundColor = Color.White;
   ((NavigationPage)Application.Current.MainPage).BarTextColor = Color.OrangeRed;
}
...