Xamarin Forms Disable swipe between pages in TabbedPage

独自空忆成欢 提交于 2019-11-30 19:33:13

You basically have two options: Either using Code Behind or XAML. I will describe both in this answer.

Code Behind

When using Code Behind, you can use the SetIsSwipePagingEnabled(bool) extension method for any given TabbedPage:

namespace App
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class MainTabbedPage : TabbedPage
    {
        public MainTabbedPage ()
        {
            InitializeComponent();

            this.On<Xamarin.Forms.PlatformConfiguration.Android>().SetIsSwipePagingEnabled(false);

            Children.Add(new PageOne());
            Children.Add(new PageTwo());
            Children.Add(new PageThree());
        }
    }
}

XAML

In XAML, you can set the IsSwipePagingEnabled property of your TabbedPage to False like this:

<TabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
        xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
        x:Class="App.MainTabbedPage"
        xmlns:android="clr-namespace:Xamarin.Forms.PlatformConfiguration.AndroidSpecific;assembly=Xamarin.Forms.Core"
        android:TabbedPage.IsSwipePagingEnabled="False"

Additional details can be found in this post.

If your using tabbed page then this one line code works

namespace App
{

 public partial class MainTabbedPage : TabbedPage
 {
    public MainTabbedPage ()
    {
        InitializeComponent();
        Xamarin.Forms.PlatformConfiguration.AndroidSpecific.TabbedPage.SetIsSwipePagingEnabled(this, false);

        Children.Add(new PageOne());
        Children.Add(new PageTwo());
        Children.Add(new PageThree());
    }
  }
}

EDIT:

You can also call the following code from your Xamarin.Forms.TabbedPage:

 this.On<Xamarin.Forms.PlatformConfiguration.Android>().SetIsSwipePagingEnabled(false);

ORIGINAL ANSWER (still working):

Hehe i wish i've found the answer above before i hacked into mine. Anyway, if you are using a custom renderer for a tabbed page, you can include that option there:

    public class MyTabbedRenderer : TabbedPageRenderer
    {

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

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

            //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;
            }

            if (PagerLayout!=null) //now disable the swiping
            {
                var propertyInfo = PagerLayout.GetType().GetProperty("EnableGesture");
                propertyInfo.SetValue(PagerLayout, false, null);
            }

        }
...
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!