change color barBackgroundColor from MasterDetailPage

Luis_dmLuis_dm USMember ✭✭✭
edited December 2016 in Xamarin.Forms

I would like to know if it is possible to change the color of the "barBackgroundColor" of the "navigationPage", what happens is that I have a page where it has a list of colors and when you choose a color, it sends that color to the page of MasterDetail but nothing happens:,Do I need that it runs in real-time? a task?

MasterDetail.xaml

 <MasterDetailPage.Master>
        <local:MenuPage x:Name="ContePageMenu"/>
      </MasterDetailPage.Master>
      <MasterDetailPage.Detail>
        <NavigationPage x:Name="Navigator"  BarBackgroundColor="{DynamicResource ColorAzul}" BarTextColor="{DynamicResource ColorBlanco}">
          <x:Arguments>
            <local:ListaPedidosPage/>
          </x:Arguments>
        </NavigationPage>
      </MasterDetailPage.Detail>

Answers

  • AlexDunnAlexDunn USMember ✭✭✭

    Yeah you can totally do this. In fact, the BarBackgroundColor is a fully bindable property.
    You could set up a ViewModel that acts as the BindingContext for your navigation page that has a Color property. You could then update that ViewModel's Color property from your list page and it will automatically update the color of bar accordingly.

  • TiagoRodriguesNesTiagoRodriguesNes USMember ✭✭

    Have you solved your problem?
    I have a similar 1.

    I've tried multiple approaches but the bar never changes color.

  • TiagoRodriguesNesTiagoRodriguesNes USMember ✭✭

    This did the trick for me.

    XAML

    <MasterDetailPage.Detail>
        <NavigationPage x:Name="detailPage">
    

    C#

    detailPage.BarBackgroundColor = Settings.SECONDARY_COLOR;
    
  • HugoHugo USMember

    So .... this is how I did it...

        public MainPage(IEventAggregator ea)
        {
            InitializeComponent();
            MasterBehavior = MasterBehavior.Popover;
            MenuPages.Add((int)MenuItemType.Browse, (NavigationPage)Detail);
            _currentPage = (NavigationPage) Detail;
    
            ea.GetEvent<LoadSheddingStatusEvent>().Subscribe(result =>
                {
                    _currentPage.BarBackgroundColor = result.Color;
                });
        }
    
        public async Task NavigateFromMenu(int id)
        {
            if (!MenuPages.ContainsKey(id))
            {
                switch (id)
                {
                    case (int)MenuItemType.Browse:
                        var dashB = new NavigationPage(new DashboardPage());
                        dashB.BarBackgroundColor = Color.Gray;
                        MenuPages.Add(id, dashB);
                        break;
                    case (int)MenuItemType.About:
                        MenuPages.Add(id, new NavigationPage(new AboutPage()));
                        break;
                }
            }
    
            var newPage = MenuPages[id];
    
            if (newPage != null && Detail != newPage)
            {
                newPage.BarBackgroundColor = _currentPage.BarBackgroundColor;
                Detail = newPage;
                _currentPage = (NavigationPage)Detail;
    
                if (Device.RuntimePlatform == Device.Android)
                    await Task.Delay(100);
    
                IsPresented = false;
            }
        }
    
Sign In or Register to comment.