android 禁止页面点击,c# – 如何在Xamarin Forms Android中隐藏时禁用单击TabbedPage菜单项?...

尝试设置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">

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值