c# - 如何在 Xamarin Forms 中的 "re-adding"TabbedPage 子项时禁用 BottomNavigationView 切换模式?

标签 c# xamarin xamarin.forms xamarin.android

我有一个在 Android 端使用 BottomNavigationView 的 XF 应用程序,它根据用户模式删除和添加 TabbedPage 子项。下面是我的 Android 渲染器:

public class BottomTabPageRenderer : TabbedPageRenderer
{

    public BottomTabPageRenderer(Context context) : base(context) { }

    protected override void OnElementChanged(ElementChangedEventArgs<TabbedPage> e)
    {
        base.OnElementChanged(e);

        if (ViewGroup != null && ViewGroup.ChildCount > 0)
        {
            BottomNavigationMenuView bottomNavigationMenuView = FindChildOfType<BottomNavigationMenuView>(ViewGroup);

            if (bottomNavigationMenuView != null)
            {
                var shiftMode = bottomNavigationMenuView.Class.GetDeclaredField("mShiftingMode");

                shiftMode.Accessible = true;
                shiftMode.SetBoolean(bottomNavigationMenuView, false);
                shiftMode.Accessible = false;
                shiftMode.Dispose();

                for (var i = 0; i < bottomNavigationMenuView.ChildCount; i++)
                {
                    var item = bottomNavigationMenuView.GetChildAt(i) as BottomNavigationItemView;
                    if (item == null) continue;

                    item.SetShiftingMode(false);
                    item.SetChecked(item.ItemData.IsChecked);
                }

                if (bottomNavigationMenuView.ChildCount > 0) bottomNavigationMenuView.UpdateMenuView();
            }

        }

        T FindChildOfType<T>(ViewGroup viewGroup) where T : Android.Views.View
        {
            if (viewGroup == null || viewGroup.ChildCount == 0) return null;

            for (var i = 0; i < viewGroup.ChildCount; i++)
            {
                var child = viewGroup.GetChildAt(i);

                var typedChild = child as T;
                if (typedChild != null) return typedChild;

                if (!(child is ViewGroup)) continue;

                var result = FindChildOfType<T>(child as ViewGroup);

                if (result != null) return result;
            }

            return null;
        }
   }
}

当用户通过单击按钮开始游戏时,底部 5 个选项卡中的 4 个将被删除。当用户通过单击按钮再次结束游戏时,将再次添加选项卡。我试过 xxx.Children.Insert() 将标签放回原来的位置,但这给了我以下错误:

Java.Lang.IndexOutOfBoundsException: Index: 1, Size: 1

所以我得到了以下代码:

public static void ReAddChildren()
{
   mainPage.Children.Remove(gameTabPage);
   mainPage.Children.Add(homeTabPage);
   mainPage.Children.Add(helpTabPage);
   mainPage.Children.Add(settingsTabPage);
   mainPage.Children.Add(dictTabPage);
   mainPage.Children.Add(gameTabPage);
 }

这有效,但现在 shiftingMode 又回来了。任何人都知道每当我“重新添加”标签项时如何禁用它?

最佳答案

Android 刚刚更新了 28.0.0 支持库。 不确定 Xamarin 是否可以访问此 native 方法 setLabelVisibilityMode() 以禁用移位。

navBottom.setLabelVisibilityMode(LabelVisibilityMode.LABEL_VISIBILITY_LABELED);

关于c# - 如何在 Xamarin Forms 中的 "re-adding"TabbedPage 子项时禁用 BottomNavigationView 切换模式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53966972/

相关文章:

objective-c - "/Volumes"名称是否会根据系统语言而变化,如果是,如何调整?

android - 在不重新创建 Activity 的情况下在替代布局之间切换

xamarin.forms - 将选择器选择的值绑定(bind)到属性 Xamarin.Forms

c# - objective-c 是否具有类似于 c# 的泛型类型(和约束)?

c# - WebApi 在本地工作,但在发布时失败

c# - Xamarin Forms ListView 绑定(bind)

c# - 毛伊岛的不透明度错误

Xamarin 表单水平 ListView ,带有用于将日期向右、向左移动的按钮

c# - 如何在 xamarin 表单中为 ListView 的每个项目提供不同的文本颜色

c# - 如何在单个事务下获得两个工作单元