【问题标题】:How to change Shell Tabbar height如何更改 Shell Tabbar 高度
【发布时间】:2021-08-22 22:54:27
【问题描述】:

我的问题: 我使用带有底部 Tabbar 的 Shell,我只是想在 Android 和 iOS 上更改它的高度。

我的搜索结果: 我知道Tabbar 是一个 Android/iOS 内置功能,具有固定的指定高度。

似乎有一种方法可以使用“自定义渲染器”来实现这一点。我发现使用这种方法的所有项目都不适用于我的 Visual Studio(>100 错误或根本不执行任何操作)。

我很难相信我需要 >100 行代码来“仅”调整高度。有人可以提供一个可行的解决方案来更改 Shell Tabbar(底部标签)的高度吗?

编辑

【问题讨论】:

    标签: c# android xamarin.forms custom-renderer xamarin.forms.shell


    【解决方案1】:

    不幸的是,您需要在每个平台项目上创建一个自定义渲染器:

    安卓

    MyShellRenderer

    [assembly: ExportRenderer(typeof(App.AppShell), typeof(App.Droid.MyShellRenderer))]
    
    namespace App.Droid
    {
        public class MyShellRenderer : ShellRenderer
        {
            public MyShellRenderer(Context context) : base(context)
            {
            }
    
        protected override IShellBottomNavViewAppearanceTracker CreateBottomNavViewAppearanceTracker(ShellItem shellItem)
            {
                return new MyBottomNavViewAppearanceTracker(this, shellItem);
            }
        }
    }
    

    MyBottomNavViewAppearanceTracker

    class MyBottomNavViewAppearanceTracker : ShellBottomNavViewAppearanceTracker
        {
            public MyBottomNavViewAppearanceTracker(IShellContext shellContext, ShellItem shellItem) : base(shellContext, shellItem)
            {
            }
            public override void SetAppearance(BottomNavigationView bottomView, IShellAppearanceElement appearance)
            {
                bottomView.LayoutParameters.Height = 400;
                base.SetAppearance(bottomView, appearance);
            }
        }
    

    编辑:

    iOS 部分归功于 https://forums.xamarin.com/discussion/169894/how-to-change-shell-height-of-flyout-items-at-the-bottom-of-the-page

    iOS

    MyShellRenderer

    [assembly: ExportRenderer(typeof(App.AppShell), typeof(App.iOS.MyShellRenderer))]
    
    namespace App.iOS
    {
        public class MyShellRenderer : ShellRenderer
        {
    
            protected override IShellTabBarAppearanceTracker CreateTabBarAppearanceTracker()
            {
                return new MyCreateTabBarAppearanceTracker();
            }
        }
    }
    

    CreateTabBarAppearanceTracker

    class CreateTabBarAppearanceTracker : ShellTabBarAppearanceTracker
        {
        
            public override void SetAppearance(UITabBarController controller, ShellAppearance appearance)
            {
                UITabBar tabBar = controller.TabBar;
                int tabBarHeight = 100;
    
                if (tabBar.Frame.Height != tabBarHeight)
                {
                    tabBar.Frame = new CGRect(tabBar.Frame.X, tabBar.Frame.Y + (tabBar.Frame.Height - tabBarHeight), tabBar.Frame.Width, tabBarHeight); 
                }
            }
        }
    

    附加功能

    您可能遗漏了一些可以解释异常大量错误的命名空间。

    找不到您是否缺少 using 指令或程序集引用

    通常人们不会在答案中包含命名空间(using 语句),因为大多数时候命名空间是显而易见的,并且可以由 Visual Studio intellisense 轻松处理。

    只需将光标放在或悬停在红色下划线语句上,就会出现一个小工具提示

    然后点击“显示潜在修复”或其中一个键盘快捷键,选择适当的修复(通常是建议列表中的第一个),在这种情况下,智能感知将自动添加所需的命名空间。

    【讨论】:

    • 对于 iOS 你需要做类似的事情
    • “BottomNavigationView”带有红色下划线 你知道为什么吗?它给出了一个错误:没有找到命名空间
    • 你必须有这个 using 语句using Google.Android.Material.BottomNavigation; 通常由智能感知处理这个。
    • OMFG 谢谢!!我不知道为什么每个代码示例都排除了 using 指令......
    • 因为大多数时候命名空间是显而易见的,并且在单击“ctrl+”时由 Visual Studio intlisense 处理。或 "ctrl+enter" 当光标位于未定义语句上时 intelisense 将自动建议并添加所需的命名空间。
    【解决方案2】:

    我发现的每个代码示例都排除了 using 指令。就我而言,我需要以下 3 个我无法找到的。感谢@Cfun 的回答,上面的代码可以添加:

    using Google.Android.Material.BottomNavigation;
    using Xamarin.Forms;
    using Xamarin.Forms.Platform.Android;
    

    【讨论】:

    • 仅供参考:当有一个带有红色下划线的名称时,您将鼠标悬停在它上面,您应该会看到一个带有更正选项的图标。这些选项之一是添加所需的 using 指令。这就是为什么人们通常不会在小代码 sn-p 中为“使用”而烦恼。
    猜你喜欢
    • 1970-01-01
    • 2020-01-21
    • 2020-01-24
    • 2019-05-19
    • 2021-07-11
    • 2020-10-28
    • 1970-01-01
    • 2021-03-01
    • 2014-09-21
    相关资源
    最近更新 更多