我有一个在 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/