简体   繁体   English

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

[英]Change Navigation Bar Color for Single Page in Xamarin

In my Xamarin app, I set the color of Navigation Bar (Status bar / top bar) in styles.xml page.在我的 Xamarin 应用程序中,我在styles.xml页面中设置了导航栏(状态栏/顶部栏)的颜色。 And the color of navigation bar in all page changes to White .并且所有页面中导航栏的颜色都变为白色

styles.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>

Now, I just want to change the color of navigation bar in single content page to Black .现在,我只想将单个内容页面中导航栏的颜色更改为Black

I followed this answer by pasting the code in the content page, but color didn't change.我通过在内容页面中粘贴代码来遵循这个答案,但颜色没有改变。

((NavigationPage)Application.Current.MainPage).BarBackgroundColor = Color.Black;
((NavigationPage)Application.Current.MainPage).BarTextColor = Color.Black;

UPDATE更新

在此处输入图像描述

Try this,尝试这个,

In App.xaml for all pages在 App.xaml 中适用于所有页面

<Application.Resources>
    <ResourceDictionary>


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

        </Style>

    </ResourceDictionary>
</Application.Resources>

In lets say Page 2 with Color Black, and with OnDisappearing White again for all在让我们说第 2 页颜色为黑色,并再次为所有人使用 OnDisappearing White

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM