简体   繁体   English

Xamarin Forms特定于平台的SetBarSelectedItemColor无效

[英]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 多个来源( Xamarin支持Xamarin docs )表明有一种方法可以做到这一点,但是它必须作为特定于Android平台的平台来完成

On<Android>().SetBarSelectedItemColor(color)

However, I'm testing this from the stock android template in Visual studio and it has no effect. 但是,我正在Visual Studio中的普通android模板中对此进行测试,它没有效果。 It doesn't matter if I run it in the TabbedPage constructor, or as an event later. 不管我是在TabbedPage构造函数中运行它,还是稍后将其作为事件运行。

Version info: 版本信息:

Xamarin Forms: 3.5.0.129452 Xamarin形式:3.5.0.129452
Visual Studio: 15.9.7 的Visual Studio:15.9.7
Xamarin.Android SDK: 9.1.7.0 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) MainPage.xaml.cs(注意:App.OnChange确实被触发,并且代码按预期执行)

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 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: 官方文档中 ,您可以为BarSelectedItem设置静态颜色,如下所示:

<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: 通过使用DynamicResource ,它可以动态设置BarSelectedItemColor:

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: 可以在ContentPage设置要更改颜色的位置,如下所示:

 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: 并应在Xaml中删除此属性:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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