有没有办法定制Xamarin.Forms.TabbedPage对象选项卡的颜色模式,以便它不采用目标平台的默认外观和感觉?我想要更改字体颜色,背景颜色和当前选中选项卡的颜色。
[assembly: ExportRenderer(typeof(TabbedPage), typeof(TabbedPageRenderer))]
namespace MyApp.iOS
{
public class TabbedPageRenderer : TabbedRenderer
{
protected override void OnElementChanged(VisualElementChangedEventArgs e)
{
base.OnElementChanged(e);
TabBar.TintColor = UIColor.White;
TabBar.BarTintColor = UIColor.Black;
TabBar.BackgroundColor = UIColor.Gray;
}
}
}
在 Xamarin.iOS 项目中,只需将此类粘贴即可。
对于 Xamarin.Android,你也可以使用自定义渲染器来实现相同的功能。 Android 版本的自定义渲染器与 iOS 版本不同。
来晚了!
现在您可以按以下方式更改选项卡页面的背景颜色:
BarBackgroundColor = Color.Black;
下面的链接可能会更有帮助:
如何在Xamarin.Droid中更改选项卡页指示器的颜色?
http://thatcsharpguy.com/post/platformtabbedpage-xamarin-forms-en/
class MainPage : TabbedPage
{
LoginManager app;
public MainPage(LoginManager ilm)
{
app = ilm;
Title = "Infrastructure";
Icon = "server.png";
this.BarTextColor = Color.White;
this.BarBackgroundColor = Color.Blue;
Children.Add(new AssetsView());
Children.Add(new ServiceView());
ToolbarItem tbi = new ToolbarItem() {
Text = "Logout",
Order = ToolbarItemOrder.Secondary,
Priority = 0,
};
AssetView 代码:
public AssetView()
{
Title = "Assets";
this.BackgroundColor = Color.FromHex("D3D3D3");
list = new AssetsList();
searchbar = new SearchBar()
{
Placeholder = "Search",
TextColor = Color.Black,
BackgroundColor = Color.White,
CancelButtonColor = Color.Black,
PlaceholderColor = Color.Black
};
ServiceView 代码:
public class ServiceView : ContentPage
{
ServiceList list;
SearchBar searchbar;
public ServiceView()
{
Title = "Services";
this.BackgroundColor = Color.FromHex("D3D3D3");
list = new ServiceList();
searchbar = new SearchBar()
{
Placeholder = "Search",
TextColor = Color.Black,
BackgroundColor = Color.White,
CancelButtonColor = Color.Black,
PlaceholderColor = Color.Black
};
<TabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
xmlns:views="clr-namespace:SilCoyLuhn.Views"
x:Class="SilCoyLuhn.Views.MainPage"
BarBackgroundColor="{StaticResource Primary}"
BarTextColor="{StaticResource LightTextColor}">
<TabbedPage.Resources>
<ResourceDictionary>
<Color x:Key="Primary">#9DD69F</Color>
<Color x:Key="Accent">#E1F4E0</Color>
<Color x:Key="LightTextColor">#999999</Color>
</ResourceDictionary>
</TabbedPage.Resources>
</TabbedPage>
<TabbedPage.Resources>
中,我定义了静态资源,用作 BarBackgroundColor
和 BarTextColor
。