Xaml 选定选项卡的Xamarin表单外壳自定义图标

Xaml 选定选项卡的Xamarin表单外壳自定义图标,xaml,xamarin.forms,xamarin.android,xamarin.ios,custom-renderer,Xaml,Xamarin.forms,Xamarin.android,Xamarin.ios,Custom Renderer,我正在玩很酷的xamarin shell,但我没有找到一种方法来更改所选选项卡的图标 <TabBar Route="sections"> <Tab Title="home"> <Tab.Icon> <FontImageSource FontFamily="{StaticResource AppIcons}" Glyph="{x:Static framework:Icons.HomePage}" />

我正在玩很酷的xamarin shell,但我没有找到一种方法来更改所选选项卡的图标

<TabBar Route="sections">
    <Tab Title="home">
        <Tab.Icon>
            <FontImageSource FontFamily="{StaticResource AppIcons}" Glyph="{x:Static framework:Icons.HomePage}" />
        </Tab.Icon>
        <ShellContent ContentTemplate="{DataTemplate home:HomePage}" Route="home" />
    </Tab>
目标是仅在选中此选项卡时才使用Icons.HomePageFilled而不是Icons.HomePage。同样的逻辑也适用于其他选项卡

我想我在网上找到的解决方案中迷失了方向。他们讨论了自定义渲染器可hellablelayoutpearancetracker、视觉状态、效果等。。。 但是我不知道它是否可行,理想的解决方案是什么

您需要使用shell在每个平台上自定义tabbar selected图标

在iOS中,重写CreateTabarAppearanceTracker方法:

在Android中,重写CreateBoottomNavViewAppearanceTracker方法:


我上传了一个示例项目,您可以查看。

您可以将背景和实体厚度的Icons.HomePage设置为透明,然后您可以将Shell.TabBarForegroundColor设置为您希望填充的颜色,透明部分将替换为该颜色。也许这会有帮助!这太棒了!是否仍然可以根据文件名动态执行此操作?就像:
[assembly: ExportRenderer(typeof(AppShell), typeof(MyShellRenderer))]
namespace App30.iOS
{
    public class MyShellRenderer : ShellRenderer
    {
        protected override IShellSectionRenderer CreateShellSectionRenderer(ShellSection shellSection)
        {
            var renderer = base.CreateShellSectionRenderer(shellSection);
            if (renderer != null)
            {

            }
            return renderer;
        }

        protected override IShellTabBarAppearanceTracker CreateTabBarAppearanceTracker()
        {
            return new CustomTabbarAppearance();
        }
    }

    public class CustomTabbarAppearance : IShellTabBarAppearanceTracker
    {
        public void Dispose()
        {

        }

        public void ResetAppearance(UITabBarController controller)
        {

        }

        public void SetAppearance(UITabBarController controller, ShellAppearance appearance)
        {
            UITabBar myTabBar = controller.TabBar;

            if (myTabBar.Items != null)
            {
                UITabBarItem itemOne = myTabBar.Items[0];

                itemOne.Image = UIImage.FromBundle("tab_about.png");
                itemOne.SelectedImage = UIImage.FromBundle("tab_feed.png");


                UITabBarItem itemTwo = myTabBar.Items[1];

                itemTwo.Image = UIImage.FromBundle("tab_feed.png");
                itemTwo.SelectedImage = UIImage.FromBundle("tab_about.png");

                //The same logic if you have itemThree, itemFour....
            }

        }

        public void UpdateLayout(UITabBarController controller)
        {

        }
    }
}
[assembly: ExportRenderer(typeof(AppShell), typeof(MyShellRenderer))]
namespace App30.Droid
{
    public class MyShellRenderer : 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)
        {
            IMenu myMenu = bottomView.Menu;

            IMenuItem myItemOne = myMenu.GetItem(0);

            if (myItemOne.IsChecked)
            {
                myItemOne.SetIcon(Resource.Drawable.tab_about);
            }
            else
            {
                myItemOne.SetIcon(Resource.Drawable.tab_feed);
            }

            //The same logic if you have myItemTwo, myItemThree....

        }
    }
}