xaml - Xamarin forms 选定选项卡的 Shell 自定义图标

标签 xaml xamarin.forms xamarin.android xamarin.ios custom-renderer

我正在玩 COOL 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。相同的逻辑应该适用于其他选项卡。

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

最佳答案

您需要使用 custom renderer shell 自定义每个平台上的选项卡选择图标。

在 iOS 中,覆盖 CreateTabBarAppearanceTracker 方法:

[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)
        {

        }
    }
}

在 Android 中,覆盖 CreateBottomNavViewAppearanceTracker 方法:

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

        }
    }
}

我上传了一个示例项目 here你可以检查它。

关于xaml - Xamarin forms 选定选项卡的 Shell 自定义图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58635349/

相关文章:

c# - DoubleTapped 事件在 WebView 上不起作用

c# - 在 Windows 应用商店应用程序的 TextBlock 和 PasswordBox 中垂直和水平居中文本

c# - 以编程方式向 UWP 应用添加按钮

c# - SQLite 中的 MissingMethodException

c# - Xamarin Android - 从另一个 Activity 更新一个 Activity 的 UI 控制

c# - 如何将 View 内的命令绑定(bind)到 MvvmCross 中的 ViewModel

c# - 在虚拟化列表框中延迟加载图像

Xamarin.Forms 版本

Xamarin 表单和 Entity Framework 核心 NetStandard

android - xamarin android 中的按钮对齐