Xamarin.Forms with Shell:有没有办法指定图标';s颜色仅适用于活动选项卡?
我正在开发一个基于Shell的Xamarin.Forms应用程序,我正在使用字体图标作为Xamarin.Forms with Shell:有没有办法指定图标';s颜色仅适用于活动选项卡?,xamarin.forms,fonts,tabbar,xamarin.shell,Xamarin.forms,Fonts,Tabbar,Xamarin.shell,我正在开发一个基于Shell的Xamarin.Forms应用程序,我正在使用字体图标作为选项卡的图标: <TabBar> <ShellContent Title="Home" Route="HomePage" ContentTemplate="{DataTemplate local:HomePage}"> <ShellContent.Icon> <
选项卡的图标:
<TabBar>
<ShellContent Title="Home" Route="HomePage" ContentTemplate="{DataTemplate local:HomePage}">
<ShellContent.Icon>
<FontImageSource Glyph="{StaticResource FasIconHome}" FontFamily="FontAwesomeSolid" />
</ShellContent.Icon>
</ShellContent>
</TabBar>
我只想为活动选项卡指定颜色,但只为图标指定颜色,而不为文本指定颜色,正如我们在Airbnb上看到的:
我在Shell设置中未找到任何选项:
<Setter Property="Shell.BackgroundColor" Value="White" />
<Setter Property="Shell.ForegroundColor" Value="Black" />
<Setter Property="Shell.TitleColor" Value="Black" />
<Setter Property="Shell.DisabledColor" Value="{StaticResource Gray-300}" />
<Setter Property="Shell.UnselectedColor" Value="{StaticResource Gray-300}" />
<Setter Property="Shell.TabBarBackgroundColor" Value="White" />
<Setter Property="Shell.TabBarForegroundColor" Value="{StaticResource Gray-300}"/>
<Setter Property="Shell.TabBarUnselectedColor" Value="#95000000" />
<Setter Property="Shell.TabBarTitleColor" Value="Black" />
可能吗?在您的案例中,您使用FontImageSource设置选项卡项的图标。但是,如果要设置特定项目图标的颜色,则需要提前下载不同颜色的图标,并将图标放置在本机平台中(iOS中为Asset,Android中为Drawable)。并使用自定义渲染器进行设置
在iOS中
在Android中
您好,我们可以使用ShellRenderer进行一次尝试,但是这样做时会出现一些问题。如果我已经解决了,我会在这里分享答案。谢谢你的反馈@JuniorJiang MSFT
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using xxx;
using xxx.iOS;
using Foundation;
using UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
[assembly: ExportRenderer(typeof(AppShell), typeof(ShellCustomRenderer))]
namespace xxx.iOS
{
public class ShellCustomRenderer : ShellRenderer
{
protected override IShellTabBarAppearanceTracker CreateTabBarAppearanceTracker()
{
return new TabBarAppearance();
}
}
public class TabBarAppearance : IShellTabBarAppearanceTracker
{
public void Dispose()
{
}
public void ResetAppearance(UITabBarController controller)
{
}
public void SetAppearance(UITabBarController controller, ShellAppearance appearance)
{
UITabBar myTabBar = controller.TabBar;
if (myTabBar.Items != null)
{
var item = myTabBar.Items[0];
//default icon
item.Image = UIImage.FromBundle("xxx.png").ImageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal);
//selected icon
item.SelectedImage = UIImage.FromBundle("xxx.png").ImageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal);
}
}
public void UpdateLayout(UITabBarController controller)
{
}
}
}
[assembly: ExportRenderer(typeof(AppShell), typeof(ShellCustomRenderer ))]
namespace xxx.Droid
{
public class ShellCustomRenderer : ShellRenderer
{
public MyShellRenderer(Context context) : base(context)
{
}
protected override IShellBottomNavViewAppearanceTracker CreateBottomNavViewAppearanceTracker(ShellItem shellItem)
{
return new CustomBottomNavAppearance();
}
}
public class CustomBottomNavAppearance : IShellBottomNavViewAppearanceTracker
{
public void Dispose()
{
}
public void ResetAppearance(BottomNavigationView bottomView)
{
}
public void SetAppearance(BottomNavigationView bottomView, ShellAppearance appearance)
{
bottomView.ItemIconTintList = null;
IMenu myMenu = bottomView.Menu;
IMenuItem myItemOne = myMenu.GetItem(0);
if (myItemOne.IsChecked)
{
myItemOne.SetIcon(Resource.Drawable.xxx); // selected icon
}
else
{
myItemOne.SetIcon(Resource.Drawable.xxx); //default icon
}
}
}
}