Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xamarin Forms Disable swipe between pages in TabbedPage

Is there a way to disable the swiping between TabbedPage on Android in Xamarin Forms?

XAML:

<TabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="App.MainTabbedPage">
</TabbedPage>

C#:

using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace App
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class MainTabbedPage : TabbedPage
    {
        public MainTabbedPage ()
        {
            InitializeComponent();
            Children.Add(new PageOne());
            Children.Add(new PageTwo());
            Children.Add(new PageThree());
        }
    }
}

Current behavior is that you can simply swipe to switch between the pages. But I'd like to disable that... I found this link but I can't seem to implement it in my code. Any help appreciated

like image 328
Helder De Baere Avatar asked Aug 22 '17 14:08

Helder De Baere


Video Answer


2 Answers

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.

like image 198
Demitrian Avatar answered Sep 19 '22 03:09

Demitrian


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());
    }
  }
}
like image 26
seem7teen Avatar answered Sep 20 '22 03:09

seem7teen