How can I change a navbar color of single page?

EurusEurus Member ✭✭

I have to change color of only one navbar in my App.
Until now I handle color of my navbars in this way:

BarBackgroundColor = Color. Blue;
in this case I can't.

Before i did:

Detail= new NavigationBar(new Page)
{
BarBackgroundColor = Color. Blue
}

Now my page is not a Detail of a MasterPage. Is just a content page linked to a Detail.
In the first page i have a method:

async void Handle_EditProfile(object sender, Event Args e){

  await Navigation.PushAsync(new EditProfile());

 // I wanna set here color of my navbar ( I wanna change color navbar only for EditProfile Page)

}

Answers

  • ColeXColeX Member, Xamarin Team Xamurai

    Find current NavigationPage and change BarBackgroundColor in OnAppearing method , change back in OnDisappearing method.

    EditProfile Page

     protected override void OnAppearing()
        {
            base.OnAppearing();
    
            NavigationPage n = (NavigationPage)((MasterDetailPage)Application.Current.MainPage).Detail;
            n.BarBackgroundColor = Color.Red;
        }
    
    
        protected override void OnDisappearing()
        {
            base.OnDisappearing();
            NavigationPage n = (NavigationPage)((MasterDetailPage)Application.Current.MainPage).Detail;
            n.BarBackgroundColor = Color.Blue;
        }
    

  • chetanrawatchetanrawat USMember ✭✭✭

    Hi, @Eurus

    Try this

    public class App : Application
    {
        public App()
        {
            var navPage = new NavigationPage(new Page());
            navPage.BindingContext = new { BarColor = Color.Blue };
            navPage.SetBinding(NavigationPage.BarBackgroundColorProperty, new Binding("BarColor"));
    
            this.MainPage = navPage;
        }
    }
    
  • EurusEurus Member ✭✭

    @ColeX said:
    Find current NavigationPage and change BarBackgroundColor in OnAppearing method , change back in OnDisappearing method.

    EditProfile Page

     protected override void OnAppearing()
        {
            base.OnAppearing();
    
            NavigationPage n = (NavigationPage)((MasterDetailPage)Application.Current.MainPage).Detail;
            n.BarBackgroundColor = Color.Red;
        }
    
    
        protected override void OnDisappearing()
        {
            base.OnDisappearing();
            NavigationPage n = (NavigationPage)((MasterDetailPage)Application.Current.MainPage).Detail;
            n.BarBackgroundColor = Color.Blue;
        }
    

    > @ColeX said:
    Find current NavigationPage and change BarBackgroundColor in OnAppearing method , change back in OnDisappearing method.

    EditProfile Page

     protected override void OnAppearing()
        {
            base.OnAppearing();
    
            NavigationPage n = (NavigationPage)((MasterDetailPage)Application.Current.MainPage).Detail;
            n.BarBackgroundColor = Color.Red;
        }
    
    
        protected override void OnDisappearing()
        {
            base.OnDisappearing();
            NavigationPage n = (NavigationPage)((MasterDetailPage)Application.Current.MainPage).Detail;
            n.BarBackgroundColor = Color.Blue;
        }
    

    My page it isn a MasterDetail page. I have a Master, if I click a button I am redirected on a Detail, When I am on Detail I click a button that push me on a Page EditProfile, which is just a Content Page, no a Detail. I Just wanna change color of that navbar(EditProfile) ! With your code all of my navbars become of another color!

  • EurusEurus Member ✭✭

    @chetanrawat said:
    Hi, @Eurus

    Try this

    public class App : Application
    {
        public App()
        {
            var navPage = new NavigationPage(new Page());
            navPage.BindingContext = new { BarColor = Color.Blue };
            navPage.SetBinding(NavigationPage.BarBackgroundColorProperty, new Binding("BarColor"));
    
            this.MainPage = navPage;
        }
    }
    

    Nope.. I Just tell you that I wanna change color of a specific page. I can't modify that and add a navpage on MainPage! my mainPage stars from MasterPage

  • EurusEurus Member ✭✭

    @Eurus said:

    @chetanrawat said:
    Hi, @Eurus

    Try this

    public class App : Application
    {
        public App()
        {
            var navPage = new NavigationPage(new Page());
            navPage.BindingContext = new { BarColor = Color.Blue };
            navPage.SetBinding(NavigationPage.BarBackgroundColorProperty, new Binding("BarColor"));
    
            this.MainPage = navPage;
        }
    }
    

    Nope.. I Just tell you that I wanna change color of a specific page. I can't modify that and add a navpage on MainPage! my mainPage starts from MasterPage

  • ColeXColeX Member, Xamarin Team Xamurai
    edited April 10

    @Eurus

    If possible could you provide a basic simple sample so that i can test on your code directly.

Sign In or Register to comment.