gpt4 book ai didi

xamarin.forms - TabbedPage 隐藏所有标签

转载 作者:行者123 更新时间:2023-12-04 01:59:00 25 4
gpt4 key购买 nike

我是 Xamarin 的新手,想创建一个包含多个页面的应用程序,这些页面可通过单击按钮导航到。我已经弄清楚如何通过代码导航到所需的页面,但我正在努力隐藏选项卡以禁用用户导航。

我正在使用 TabbedPage 有我的主页,然后添加 8 个 NavigationPages 作为子页面。

如何隐藏标签页?

最佳答案

要在 Xamarin.Forms 中隐藏选项卡,您需要在 native 项目中包含自定义呈现器。我没有使用 UWP atm,所以只是为 iOS 和 Android 发布渲染器,它们完全隐藏了标签栏。更改我的命名空间并考虑可能将选项卡式页面子类化,这样您就不会在应用程序中隐藏所有选项卡,而只隐藏选定的选项卡。

iOS 渲染器:

using System;
using AppoMobi.iOS;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
using UIKit;

[assembly: ExportRenderer(typeof(TabbedPage), typeof(HiddenTabbedPageRenderer))]
namespace AppoMobi.iOS
{
//***************************************************************************
public class HiddenTabbedPageRenderer : TabbedRenderer
//***************************************************************************
{

private UITabBarController tabbarController { get; set; }
private TabbedPage CurrentTabbedPage { get; set; }

//-------------------------------------------------------------
protected override void OnElementChanged(VisualElementChangedEventArgs e)
//-------------------------------------------------------------
{
base.OnElementChanged(e);

if (e.OldElement != null)
{
//release any stuff here
}

if (e.NewElement != null)
{
tabbarController = (UITabBarController) this.ViewController;
CurrentTabbedPage = (TabbedPage) e.NewElement;
}
else
{
CurrentTabbedPage = (TabbedPage) e.OldElement;
}

//the following commented code is not working
//as Forms as it just leaves empty white space
//instead of hidden tabbedbar:
// if (tabbarController != null)
// tabbarController.TabBar.Hidden = true;
}

//just hide tabbar by setting its height to zero
// credits:
// https://stackoverflow.com/a/26255545/7149454
// "how to change UITabBar height"
private nfloat newHeight = 0; //change tabbed bar height to this value
//-------------------------------------------------------------------
public override void ViewWillLayoutSubviews()
//-------------------------------------------------------------------
{
if (tabbarController != null)
{
var tabFrame = tabbarController.TabBar.Frame; //self.TabBar is IBOutlet of your TabBar
tabFrame.Height = newHeight;
tabFrame.Offset(0, tabbarController.View.Frame.Height - newHeight);
tabbarController.TabBar.Frame = tabFrame;
}
base.ViewWillLayoutSubviews();
}
}
}

安卓渲染器:

using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
using System;
using Android.Content;
using Android.Support.Design.Widget;
using Android.Support.V4.View;
using Android.Views;
using AppoMobi.Droid;
using Xamarin.Forms.Platform.Android.AppCompat;

[assembly: ExportRenderer(typeof(TabbedPage), typeof(MyTabbedRenderer))]
namespace AppoMobi.Droid
{
//****************************************************
public class MyTabbedRenderer : TabbedPageRenderer, TabLayout.IOnTabSelectedListener
//****************************************************
{

private TabLayout TabsLayout { get; set; }
private ViewPager PagerLayout { get; set; }
private NiftyTabbedPage CurrentTabbedPage { get; set; }

//-------------------------------------------------------------------
public MyTabbedRenderer(Context context) : base(context)
//-------------------------------------------------------------------
{
}

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

if (e.OldElement != null)
{
//cleanup here
}

if (e.NewElement != null)
{
CurrentTabbedPage = (NiftyTabbedPage)e.NewElement;
}
else
CurrentTabbedPage = (NiftyTabbedPage)e.OldElement;

//find the pager and tabs
for (int i = 0; i < ChildCount; ++i)
{
Android.Views.View view = (Android.Views.View)GetChildAt(i);
if (view is TabLayout)
TabsLayout = (TabLayout)view;
else
if (view is ViewPager) PagerLayout = (ViewPager)view;
}

}


//-------------------------------------------------------------------------------
protected override void OnLayout(bool changed, int l, int t, int r, int b)
//-------------------------------------------------------------------------------
{
TabsLayout.Visibility = ViewStates.Gone;

base.OnLayout(changed, l, t, r, b);
}
}


}

关于xamarin.forms - TabbedPage 隐藏所有标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48658921/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com