C# 更改Xamarin中单个页面的导航栏颜色

C# 更改Xamarin中单个页面的导航栏颜色,c#,.net,xamarin,xamarin.forms,C#,.net,Xamarin,Xamarin.forms,在我的Xamarin应用程序中,我在style.xml页面中设置了导航栏(状态栏/顶部栏)的颜色。所有页面中导航栏的颜色都变为白色 styles.xml <?xml version="1.0" encoding="utf-8" ?> <resources> <style name="MainTheme" parent="MainTheme.Base"> </styl

在我的Xamarin应用程序中,我在
style.xml
页面中设置了导航栏(状态栏/顶部栏)的颜色。所有页面中导航栏的颜色都变为白色

styles.xml

<?xml version="1.0" encoding="utf-8" ?>
<resources>

  <style name="MainTheme" parent="MainTheme.Base">
  </style>
  <!-- Base theme applied no matter what API -->
  <style name="MainTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- colorPrimaryDark is used for the status bar -->
    <item name="colorPrimaryDark">#FFFFFF</item>
  </style>

</resources>
更新

试试这个

所有页面的App.xaml中

<Application.Resources>
    <ResourceDictionary>


        <Style TargetType="NavigationPage">
            <Setter Property="BarBackgroundColor" Value="White"/>
            <Setter Property="BarTextColor" Value="Black"   />

        </Style>

    </ResourceDictionary>
</Application.Resources>

您好,您尝试的代码是正确的。我想可能是你把密码放错地方了。是否可以重新检查BarBackgroundColor颜色重新分配的位置。您需要在需要修改导航栏颜色的屏幕的构造函数中重新分配颜色。实际上我使用的是
Window.SetStatusBarColor(Android.Graphics.color.White)位于
MainActivity.cs
page。。我删除了它,然后重试上面的代码和下面的答案中的代码。。没有任何效果。@Karamazov例如,您希望从contentpage1导航到contentpage2,因此您在contentpage2
OnAppearing()
,然后在contentpage2
OnDisappearing()
,返回默认导航栏颜色。是的。。我想要这个。。我尝试了下面答案中的代码。。但是它没有做任何改变。@Karamazov在contentpage 2
private Color barColor中添加这些代码;受保护的覆盖无效OnAppearing(){base.OnAppearing();var navigationPage=Application.Current.MainPage作为navigationPage;barColor=navigationPage.BackgroundColor;navigationPage.BarBackgroundColor=Color.Black;}
很抱歉响应太晚。。但是什么都没有改变..先生,你猜为什么它不起作用?很难说,你删除了styles.xml中的文本了吗?然后App.xaml中的第一部分和Page2.xaml.cs中的第二部分是的,我删除了文本并添加了这两个代码片段。我能想到的唯一一件事是,它在黑暗之光中的主题是一样的吗
<Application.Resources>
    <ResourceDictionary>


        <Style TargetType="NavigationPage">
            <Setter Property="BarBackgroundColor" Value="White"/>
            <Setter Property="BarTextColor" Value="Black"   />

        </Style>

    </ResourceDictionary>
</Application.Resources>
 public partial class Page2 : ContentPage
{
    public Page2()
    {
        InitializeComponent();
    }

    protected override void OnAppearing()
    {
        base.OnAppearing();
        var navigationPage = Application.Current.MainPage as NavigationPage;
        navigationPage.BarBackgroundColor = Color.Black;
    }

    private async void Button_Clicked(object sender, EventArgs e)
    {
        await Navigation.PushAsync(new Page2DetailsPage());
    }

    protected override void OnDisappearing()
    {
        base.OnDisappearing();

        var navigationPage = Application.Current.MainPage as NavigationPage;
        navigationPage.BarBackgroundColor = Color.White;
    }