Skip to content Skip to sidebar Skip to footer

Xamarin Forms Platform-Specific SetBarSelectedItemColor Has No Effect

I want to change the color of the active tab indictor dynamically. Multiple sources (Xamarin support, Xamarin docs) suggest there is a method that does just this, but it has to be

Solution 1:

From official document , you can set static color for BarSelectedItem as follow:

<TabbedPage ...
            xmlns:android="clr-namespace:Xamarin.Forms.PlatformConfiguration.AndroidSpecific;assembly=Xamarin.Forms.Core"
            android:TabbedPage.ToolbarPlacement="Bottom"
            android:TabbedPage.BarItemColor="Black"
            android:TabbedPage.BarSelectedItemColor="Red">
    ...
</TabbedPage>

Answer :

By using DynamicResource,it can set BarSelectedItemColor dynamically:

android:TabbedPage.BarSelectedItemColor="Red"

To this:

android:TabbedPage.BarSelectedItemColor="{DynamicResource BarSelectedItemColor}"

Complete sample code:

<TabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:local="clr-namespace:TabbedPageDemo;assembly=TabbedPageDemo"
            x:Class="TabbedPageDemo.TabbedPageDemoPage"
            xmlns:android="clr-namespace:Xamarin.Forms.PlatformConfiguration.AndroidSpecific;assembly=Xamarin.Forms.Core"
            android:TabbedPage.ToolbarPlacement="Bottom"
            android:TabbedPage.BarItemColor="Black"
            android:TabbedPage.BarSelectedItemColor="{DynamicResource BarSelectedItemColor}">

  <TabbedPage.Resources>
    <ResourceDictionary>
            <Color x:Key="BlueColor">Blue</Color>
            <Color x:Key="YellowColor">Yellow</Color>
        </ResourceDictionary>
  </TabbedPage.Resources>
  ...
</TabbedPage>

Where you want to change color can be set in ContentPage as follow:

 Resources["BarSelectedItemColor"] = Resources["BlueColor"];
 ...
 Resources["BarSelectedItemColor"] = Resources["YellowColor"];

If you do not need use this renderer,you should comment its reference. My Forms answer code will work.

//[assembly: ExportRenderer(typeof(TabbedPage), typeof(TabRenderer))]

 You need to comment its reference. My Forms answer code will work.

And should remove this property in Xaml:

<TabbedPage.BarTextColor>
        <OnPlatform x:TypeArguments="Color">
            <On Platform="Android" Value="Green" />
        </OnPlatform>
</TabbedPage.BarTextColor>

Post a Comment for "Xamarin Forms Platform-Specific SetBarSelectedItemColor Has No Effect"