【问题标题】:Route the swipe action inside segmentcontrol of a child page of tabbedpage在tabbedpage的子页面的segmentcontrol内路由滑动动作
【发布时间】:2018-03-15 10:09:21
【问题描述】:

所以我将 Xamarin 表单用于跨平台应用程序,主 UI 是一个选项卡式页面,其中一个页面中有一个 segmented control,段控件将切换列表视图控件的数据。问题是当我滑动屏幕时,它会在选项卡式页面的不同子页面之间切换,但我希望它在子页面内的段控件之间切换。无论如何我可以让滑动不适用于标签页,但内部段控件?

标签页的 XAML:

<?xml version="1.0" encoding="utf-8" ?>
<TabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
        xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
        xmlns:local="clr-namespace:MainApp"
        x:Class="MainApp.MainPage">
  <!--Pages can be added as references or inline-->
    <local:NewsPage Title="News" Icon="icon.png"/>
    <ContentPage Title="Guides" Icon="icon.png"/>
    <ContentPage Title="Wallets" Icon="icon.png"/>
    <ContentPage Title="Me" Icon="icon.png"/>
</TabbedPage>

新闻页面:

<controls:SegmentedControl x:Name="SegControl" TintColor="#007AFF" SelectedSegment="0"
                                       VerticalOptions="Center"
                                       Grid.Column="1" ValueChanged="SegControl_ValueChanged">
    <controls:SegmentedControl.Children>
        <controls:SegmentedControlOption Text="Type1" />
        <controls:SegmentedControlOption Text="Type2" />
    </controls:SegmentedControl.Children>
</controls:SegmentedControl>
<ListView ItemsSource="{Binding Source={StaticResource SArray}}" Grid.Row="1" Grid.Column="0" RowHeight="85">
 <ListView.ItemTemplate> ... </ListView>

【问题讨论】:

    标签: android xamarin xamarin.forms swipe uisegmentedcontrol


    【解决方案1】:

    我认为这是在 Android 上?我不确定在 iOS 上刷卡是否有效。

    一个选项是禁用在标签页上滑动。

    您可以使用自定义渲染器来做到这一点。

    [assembly: ExportRenderer(typeof(CustomTabbedPage), typeof(CustomTabbedPageRenderer))]
    namespace App1.Droid
    {
        public class CustomTabbedPageRenderer : TabbedPageRenderer
        {
    
            protected override void OnElementChanged(ElementChangedEventArgs<TabbedPage> e)
            {
    
                var info = typeof(TabbedPageRenderer).GetTypeInfo();
                var fieldInfo = info.GetField("_useAnimations", BindingFlags.Instance | BindingFlags.NonPublic);
                fieldInfo.SetValue(this, false);
                base.OnElementChanged(e);
            }
        }
    }
    

    编辑

    现在看起来这已添加到 Xamarin 表单中:

    https://github.com/xamarin/Xamarin.Forms/pull/409

    另一个编辑

    还有另一种方式。我今天学到了很多东西!

    public partial class MainPage : Xamarin.Forms.TabbedPage
    {
        public MainPage()
        {
            InitializeComponent();
    
            this.On<Xamarin.Forms.PlatformConfiguration.Android>().SetIsSwipePagingEnabled(false);
        }
    }
    

    我很高兴

    来自 XAML

    <TabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
            xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
            xmlns:android="clr-namespace:Xamarin.Forms.PlatformConfiguration.AndroidSpecific;assembly=Xamarin.Forms.Core"
            android:TabbedPage.IsSwipePagingEnabled="False"
    

    【讨论】:

    • 项目是xamarin forms但是这个问题在android上,ios标签页默认没有滑动。
    • 非常感谢,我添加了你的代码它可以工作,但现在我仍然需要弄清楚如何滑动列表视图来选择分段控件。 :)
    猜你喜欢
    • 1970-01-01
    • 2021-11-28
    • 1970-01-01
    • 2013-10-06
    • 1970-01-01
    • 1970-01-01
    • 2016-03-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多