【问题标题】:Xamarin Forms Disable swipe between pages in TabbedPageXamarin Forms 禁用 TabbedPage 中的页面之间的滑动
【发布时间】:2018-12-10 19:15:40
【问题描述】:

有没有办法在 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());
        }
    }
}

当前的行为是您只需滑动即可在页面之间切换。但我想禁用它... 我找到了this link,但我似乎无法在我的代码中实现它。任何帮助表示赞赏

【问题讨论】:

  • 您是否尝试过使用自定义渲染器?

标签: android xamarin xamarin.forms tabbedpage


【解决方案1】:

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

代码隐藏

使用 Code Behind 时,您可以对任何给定的 TabbedPage 使用 SetIsSwipePagingEnabled(bool) 扩展方法:

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 中,您可以将 TabbedPageIsSwipePagingEnabled 属性设置为 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"

更多详情请见this post

【讨论】:

  • 啊哈,就是这样。谢谢。我使用了On&lt;Android&gt;().SetIsSwipePagingEnabled(false);,它给了我一个错误,并且无法弄清楚错误是什么......
  • 奇怪的是,在禁用滑动的 TabbedPage 上滑动 SwipeView 后,SetIsSwipePagingEnabled 设置为 true,我可以再次滑动页面...
【解决方案2】:

编辑:

您也可以通过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);
            }

        }
...

【讨论】:

    【解决方案3】:

    如果您使用标签页,那么这一行代码可以工作

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

    【讨论】:

    • 传奇。效果很好,可以与 xaml 页面一起使用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-15
    • 1970-01-01
    • 2020-06-12
    • 1970-01-01
    • 2019-05-16
    • 2014-09-13
    相关资源
    最近更新 更多