尝试设置IsEnabled =“false”属性,看看它是否有效:
更新:
添加一个类BottomNavTabPageRenderer,它是您的Android自定义渲染器
[assembly: ExportRenderer(typeof(BottomNavTabPage), typeof(BottomNavTabPageRenderer))]
namespace Droid.CustomRenderers
{
public class BottomNavTabPageRenderer : TabbedPageRenderer, BottomNavigationView.IOnNavigationItemSelectedListener, BottomNavigationView.IOnNavigationItemReselectedListener
{
private bool _isShiftModeSet;
public BottomNavTabPageRenderer( Context context )
: base(context)
{
}
bool BottomNavigationView.IOnNavigationItemSelectedListener.OnNavigationItemSelected( IMenuItem item )
{
if(item.TitleFormatted.ToString().Trim()== "YourTitleString") // Disable based on Title of the item
{
return false;
}
if(!(this.Element as BottomNavTabPage).IsPageChangeEnabled) // Disable every tab
{
return false;
}
return true;
}
public void OnNavigationItemReselected( IMenuItem item )
{
}
protected override void OnLayout( bool changed, int l, int t, int r, int b )
{
base.OnLayout(changed, l, t, r, b);
try
{
if(!_isShiftModeSet)
{
var children = GetAllChildViews(ViewGroup);
if(children.SingleOrDefault(x => x is BottomNavigationView) is BottomNavigationView bottomNav)
{
bottomNav.SetShiftMode(false, false);
_isShiftModeSet = true;
}
}
}
catch(Exception e)
{
Console.WriteLine($"Error setting ShiftMode: {e}");
}
}
private List GetAllChildViews( View view )
{
if(!(view is ViewGroup group))
{
return new List { view };
}
var result = new List();
for(int i = 0; i < group.ChildCount; i++)
{
var child = group.GetChildAt(i);
var childList = new List { child };
childList.AddRange(GetAllChildViews(child));
result.AddRange(childList);
}
return result.Distinct().ToList();
}
}
}
请注意,OnNavigationItemSelected方法有两个条件,您可以根据这两个条件设置是否禁用页面中的选项卡.
在PCL项目中添加Bottom Navigation选项卡式页面类以用作标签页
public class BottomNavTabPage : Xamarin.Forms.TabbedPage
{
public static readonly BindableProperty ShiftingEnabledProperty = BindableProperty.Create(nameof(IsPageChangeEnabled),
typeof(bool),
typeof(BottomNavTabPage), false);
public bool IsPageChangeEnabled
{
get { return (bool)GetValue(ShiftingEnabledProperty); }
set { SetValue(ShiftingEnabledProperty, value); }
}
protected override void OnAppearing()
{
this.On().SetIsSwipePagingEnabled(IsPageChangeEnabled);
base.OnAppearing();
}
public BottomNavTabPage()
{
this.On().SetToolbarPlacement(ToolbarPlacement.Bottom);
}
}
为底部导航视图添加移位模式,以便它正确显示标签和图标.
public static void SetShiftMode( this BottomNavigationView bottomNavigationView, bool enableShiftMode, bool enableItemShiftMode )
{
try
{
var menuView = bottomNavigationView.GetChildAt(0) as BottomNavigationMenuView;
if(menuView == null)
{
System.Diagnostics.Debug.WriteLine("Unable to find BottomNavigationMenuView");
return;
}
var shiftMode = menuView.Class.GetDeclaredField("mShiftingMode");
shiftMode.Accessible = true;
shiftMode.SetBoolean(menuView, enableShiftMode);
shiftMode.Accessible = false;
shiftMode.Dispose();
for(int i = 0; i < menuView.ChildCount; i++)
{
var item = menuView.GetChildAt(i) as BottomNavigationItemView;
if(item == null)
continue;
item.SetShiftingMode(enableItemShiftMode);
item.SetChecked(item.ItemData.IsChecked);
}
menuView.UpdateMenuView();
}
catch(Exception ex)
{
System.Diagnostics.Debug.WriteLine($"Unable to set shift mode: {ex}");
}
}
用法:
您可以在C#和XAML中使用它,如下所示:
C#:
BottomNavTabPage tabbedPage = null;
tabbedPage = new BottomNavTabPage();
tabbedPage.IsPageChangeEnabled=false; // Important for not allowing tab change
var navigationPage = new NavigationPage(new MainPage())
{
Icon = "icon",
Title = "Schedule"
};
var navigationPage2 = new NavigationPage(new MainPage())
{
Icon = "icon",
Title = "Schedule2"
};
tabbedPage.Children.Add(navigationPage);
tabbedPage.Children.Add(navigationPage2);
this.MainPage= tabbedPage;
XAML
xmlns:Custom="clr-namespace:XamarinCertUnderstanding.CustomControls"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="XamarinCertUnderstanding.Views.MainPage">