14

有没有办法在 Xamarin Forms 中禁用 Android 上的 TabbedPage 之间的滑动?

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());
        }
    }
}

当前的行为是您可以简单地滑动以在页面之间切换。但我想禁用它......我找到了这个链接,但我似乎无法在我的代码中实现它。任何帮助表示赞赏

4

3 回答 3

28

您基本上有两个选择:使用代码隐藏或 XAML。我将在这个答案中描述两者。

代码背后

使用 Code Behind 时,您可以使用SetIsSwipePagingEnabled(bool)任何给定的扩展方法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

在 XAML 中,您可以将IsSwipePagingEnabled您的属性设置TabbedPageFalse

<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"

可以在这篇文章中找到更多详细信息。

于 2017-08-22T19:16:24.840 回答
8

编辑:

您还可以从您的 调用以下代码Xamarin.Forms.TabbedPage

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

原始答案(仍在工作):

呵呵,我希望在我入侵之前找到上面的答案。无论如何,如果您为标签页使用自定义渲染器,您可以在其中包含该选项:

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

        }
...
于 2017-10-31T16:07:22.830 回答
7

如果您使用标签页,那么这一行代码有效

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());
    }
  }
}
于 2018-05-16T05:54:33.427 回答