Xamarin Forms禁用TabbedPage中页面之间的滑动
Xamarin Forms Disable swipe between pages in 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>
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 但我似乎无法在我的代码中实现它。任何帮助表示赞赏
您基本上有两个选择:使用代码隐藏或 XAML。我将在这个答案中描述两者。
代码隐藏
使用代码隐藏时,您可以对任何给定的 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 中,您可以将 TabbedPage
的 IsSwipePagingEnabled
属性 设置为 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 中找到更多详细信息。
编辑:
您还可以从 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);
}
}
...
如果您使用标签页,那么这一行代码有效
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());
}
}
}
有没有办法在 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 但我似乎无法在我的代码中实现它。任何帮助表示赞赏
您基本上有两个选择:使用代码隐藏或 XAML。我将在这个答案中描述两者。
代码隐藏
使用代码隐藏时,您可以对任何给定的 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 中,您可以将 TabbedPage
的 IsSwipePagingEnabled
属性 设置为 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 中找到更多详细信息。
编辑:
您还可以从 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);
}
}
...
如果您使用标签页,那么这一行代码有效
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());
}
}
}