简体   繁体   中英

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 done as a platform-specific for android

On<Android>().SetBarSelectedItemColor(color)

However, I'm testing this from the stock android template in Visual studio and it has no effect. It doesn't matter if I run it in the TabbedPage constructor, or as an event later.

Version info:

Xamarin Forms: 3.5.0.129452
Visual Studio: 15.9.7
Xamarin.Android SDK: 9.1.7.0

Do platform-specifics only work under certain conditions?

Code:
Other than a few color binding experiments, it is stock code.

MainPage.xaml.cs (Note: App.OnChange does get triggered and the code is executed as expected)

using System;
using Xamarin.Forms;
using Xamarin.Forms.PlatformConfiguration.AndroidSpecific;
using Xamarin.Forms.Xaml;

namespace App1.Views
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class MainPage : Xamarin.Forms.TabbedPage
    {     
        public MainPage()
        {
            InitializeComponent();

            App.OnChange((prop, value) =>
            {
                if (prop == App.ActiveColorKey)
                {
                    On<Xamarin.Forms.PlatformConfiguration.Android>().SetBarSelectedItemColor(Color.FromHex(value));
                    On<Xamarin.Forms.PlatformConfiguration.Android>().SetBarItemColor(Color.FromHex(value));
                }
            });
        } 
    }
}

MainPage.xaml

<?xml version="1.0" encoding="utf-8" ?>
<TabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
            xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
            xmlns:views="clr-namespace:App1.Views"
            x:Class="App1.Views.MainPage" BarBackgroundColor="{DynamicResource TabColor}">

    <TabbedPage.BarTextColor>
        <OnPlatform x:TypeArguments="Color">
            <On Platform="Android" Value="Green" />
        </OnPlatform>
    </TabbedPage.BarTextColor>
    <TabbedPage.Children>
        <NavigationPage Title="Browse">
            <NavigationPage.Icon>
                <OnPlatform x:TypeArguments="FileImageSource">
                    <On Platform="iOS" Value="tab_feed.png"/>
                </OnPlatform>
            </NavigationPage.Icon>
            <x:Arguments>
                <views:ItemsPage />
            </x:Arguments>
        </NavigationPage>

        <NavigationPage Title="About dog" BarBackgroundColor="Red" BackgroundColor="Yellow">
            <NavigationPage.Icon>
                <OnPlatform x:TypeArguments="FileImageSource">
                    <On Platform="iOS" Value="tab_about.png"/>
                </OnPlatform>
            </NavigationPage.Icon>
            <x:Arguments>
                <views:AboutPage />
            </x:Arguments>
        </NavigationPage>
    </TabbedPage.Children>
</TabbedPage>

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>

Solution:

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))]

您需要评论其参考。我的表格答案代码将起作用。

And should remove this property in Xaml:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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