【问题标题】:Xamarin Forms Platform-Specific SetBarSelectedItemColor has no effectXamarin 表单平台特定的 SetBarSelectedItemColor 无效
【发布时间】:2019-07-10 01:53:37
【问题描述】:

我想动态更改活动标签指示器的颜色。多个来源(Xamarin supportXamarin docs)表明有一种方法可以做到这一点,但它必须作为特定于 android 的平台来完成

On<Android>().SetBarSelectedItemColor(color)

但是,我正在 Visual Studio 中的股票 android 模板中对此进行测试,但没有任何效果。我是在 TabbedPage 构造函数中运行它,还是稍后作为事件运行它都没有关系。

版本信息:

Xamarin 表单:3.5.0.129452
Visual Studio:15.9.7
Xamarin.Android SDK:9.1.7.0

特定平台是否仅在特定条件下有效?

代码:
除了一些颜色绑定实验之外,它是股票代码。

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

<?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>

【问题讨论】:

标签: c# android xamarin xamarin.forms


【解决方案1】:

official document,您可以为 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>

解决方案:

通过DynamicResource,可以动态设置BarSelectedItemColor:

android:TabbedPage.BarSelectedItemColor="Red"

到这里:

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

完整的示例代码:

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

ContentPage中可以设置你想改变颜色的地方,如下:

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

如果你不需要使用这个渲染器,你应该注释它的引用。我的表单答案代码将起作用。

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

并且应该在 Xaml 中移除这个属性:

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

【讨论】:

  • 不走运。即使我只是将其设置为静态资源,选定的选项卡指示器也不受影响。它保持默认的白色。也不会抛出任何错误。
  • 我的解决方案有一个 Tabbar.axml。我可以通过更改该文件来静态设置活动颜色。但是,无论是否设置了 app:tabIndicatorColor,颜色都不受 xaml 中的绑定影响
  • 如果 StaicResource 可以工作,那么 DynamicResource 可以做到。你可以展示一些关于 StaicResource 的代码。
  • 我发现问题出在哪里。您的 App1.Android 解决方案下有一个 Renderers 文件夹。 TabRenderer.cs 没有有效代码,但它绑定到 TabbedPage 类。这会影响表单中的代码显示。您需要评论其参考。我的表单答案的代码可以工作。//[assembly: ExportRenderer(typeof(TabbedPage), typeof(TabRenderer))]
  • 好吧,因为TabPage XamlTabbedPage.BarTextColor,并且您将其设置为Green。只需删除此属性即可。DynamicResource 将起作用。我已经更新了答案。如果您解决了您的问题。记得标记。谢谢。^.^
猜你喜欢
  • 1970-01-01
  • 2015-10-20
  • 1970-01-01
  • 1970-01-01
  • 2020-06-22
  • 1970-01-01
  • 2019-01-24
  • 1970-01-01
相关资源
最近更新 更多