![](/img/trans.png)
[英]How to apply Xamarin Platform-Specific EnableTranslucentNavigationBar
[英]Xamarin Forms Platform-Specific SetBarSelectedItemColor has no effect
我想动态更改活动标签指示符的颜色。 多个来源( Xamarin支持 , Xamarin 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>
在官方文档中 ,您可以为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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.