Xamarin表单禁用在TabbedPage中的页面之间滑动

Hel*_*ere 9 android xamarin xamarin.forms tabbedpage

有没有办法在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>
Run Code Online (Sandbox Code Playgroud)

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());
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

目前的行为是您只需滑动即可在页面之间切换.但是我想禁用它...我发现了这个链接,但我似乎无法在我的代码中实现它.任何帮助赞赏

Dem*_*ian 15

您基本上有两个选择:使用Code Behind或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());
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

XAML

在XAML中,你可以设置IsSwipePagingEnabled你的财产TabbedPage,以False这样的:

<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"
Run Code Online (Sandbox Code Playgroud)

其他详细信息可以在这篇文章中找到.


Nic*_*sky 6

编辑:

您也可以从以下代码中调用以下代码Xamarin.Forms.TabbedPage:

 this.On<Xamarin.Forms.PlatformConfiguration.Android>().SetIsSwipePagingEnabled(false);
Run Code Online (Sandbox Code Playgroud)

原始答案(仍然有效):

呵呵,我希望在我入侵我之前找到了上面的答案.无论如何,如果您为选项卡页面使用自定义渲染器,则可以在那里包含该选项:

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

        }
...
Run Code Online (Sandbox Code Playgroud)


see*_*een 6

如果您使用的是选项卡式页面,则此行代码有效

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());
    }
  }
}
Run Code Online (Sandbox Code Playgroud)