我有一个在BottomNavigationView
侧使用Android
的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是否可以访问此本机方法setLabelVisibilityMode()来禁用转换。
navBottom.setLabelVisibilityMode(LabelVisibilityMode.LABEL_VISIBILITY_LABELED);
在渲染器中,您可以轻松地使用Xamarin.Forms中的此扩展方法来管理移位模式。
所以你会得到:
using static Xamarin.Forms.Platform.Android.BottomNavigationViewUtils;
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)
{
// use extension method from XF
bottomNavigationMenuView.SetShiftMode(false, false);
}
}
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;
}
}
}
正如Pawel所提到的,您可以轻松使用Xamarin.Forms中的扩展方法来管理shift模式。但是这个对我有用
public class CustomTabbedPageRenderer : TabbedPageRenderer
{
public CustomTabbedPageRenderer(Context context) : base(context)
{
}
protected override void OnElementChanged(ElementChangedEventArgs<TabbedPage> e)
{
base.OnElementChanged(e);
if (ViewGroup != null && ViewGroup.ChildCount > 0)
{
BottomNavigationView bottomNavigationView = FindChildOfType<BottomNavigationView>(ViewGroup);
if (bottomNavigationView != null)
{
// use extension method from XF
bottomNavigationView.SetShiftMode(false, false);
}
}
}
private 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;
}
protected override void DispatchDraw(Canvas canvas)
{
base.DispatchDraw(canvas);
}
}