locked
Change NavigationPage Color for a specific page RRS feed

  • Question

  • User31568 posted

    Hi,

    I am using this code:

    (App.Current.MainPage as NavigationPage).BarBackgroundColor = Color.FromHex("#000000"); (App.Current.MainPage as NavigationPage).BarTextColor = Color.FromHex("#ffffff");

    to change the NavigationPage color of the MainPage

    But how can I add to a specific page, like this for a Signin page for example:

    public Signin()
    {
        InitializeComponent();
    
        (App.Current.THIS_PAGE as NavigationPage).BarBackgroundColor = Color.FromHex("#000000");
        (App.Current.THIS_PAGE as NavigationPage).BarTextColor = Color.FromHex("#ffffff");
    }
    

    Thanks, Jassim

    Thursday, January 30, 2020 11:50 PM

All replies

  • User390978 posted

    @JassimRahma ,

    Refer the below discussion https://forums.xamarin.com/discussion/95943/how-to-change-navigation-bar-background-color-for-each-content-page

    Friday, January 31, 2020 4:31 AM
  • User387683 posted

    You could try the code below.

    App.cs

      public NavigationPage page1 { get; set; }
        public NavigationPage page2 { get; set; }
        public App()
        {
            InitializeComponent();
    
            page1 = new NavigationPage(new Page1());
            page1.BackgroundColor = Color.AliceBlue;
            page1.BarBackgroundColor = Color.Red;
            page1.BarTextColor = Color.Blue;
    
            page2 = new NavigationPage(new Page2());
            page2.BarBackgroundColor = Color.Green;
            page2.BarTextColor = Color.Accent;
    
            MainPage = new NavigationPage(new MainPage()); ;
        }
    

    MainPage.xaml.cs

     private async void GoToPage1_Clicked(object sender, EventArgs e)
        {
            await Navigation.PushAsync((new App()).page1);
    
        }
    
        private async void GoToPage2_Clicked(object sender, EventArgs e)
        {
            await Navigation.PushAsync((new App()).page2);
        }
    

    I have upload the source code, you could download to check.

    Friday, January 31, 2020 7:45 AM
  • User385619 posted

    Didn't try it but perhaps you can use Apps PageAppearing event: https://docs.microsoft.com/en-us/dotnet/api/xamarin.forms.application.pageappearing?view=xamarin-forms

    Something like this:

    App.PageAppearing += (page)=>
    {
        Color barColor = page is MyPageWithADifferentColor?OTHERCOLOR:NORMALCOLOR;
        Color barTextColor = page is MyPageWithADifferentColor?OTHERCOLOR:NORMALCOLOR;
        page.BarBackgroundColor = barColor;
        page.BarTextColor = barTextColor;   
    }
    
    Friday, January 31, 2020 8:10 AM