【问题标题】:PanGestureRecognizer does not work in contentviewPanGestureRecognizer 在 contentview 中不起作用
【发布时间】:2021-03-01 03:11:24
【问题描述】:

您好,当用户滑动内容以查看内容时,我想这样做,但不幸的是,我的代码不起作用,请帮助。我尝试使用 PanGestureRecognizer,但我不知道但我不知道我是否做得对

     <controls:CustomFrame CornerRadius="25,25,0,0" Margin="0" Padding="10" HorizontalOptions="FillAndExpand"  VerticalOptions="FillAndExpand">
        <ScrollView>
    <AbsoluteLayout BackgroundColor="Silver" AbsoluteLayout.LayoutBounds="0,1,1,1">
        <StackLayout x:Name="bottomDrawer" AbsoluteLayout.LayoutBounds="0.5,1.00,0.9,0.04" AbsoluteLayout.LayoutFlags="All">
            <StackLayout.GestureRecognizers>
                <PanGestureRecognizer PanUpdated="PanGestureHandler" />
            </StackLayout.GestureRecognizers>
            <CollectionView ItemsSource="{Binding MyPins}"  x:Name="ListPlaces"    
                   SelectionMode="None">
                      SOMETHING
            </CollectionView>
        </StackLayout>
    </AbsoluteLayout>
    </ScrollView>
    </controls:CustomFrame>

后面的代码

   public partial class PlacesList : ContentView, INotifyPropertyChanged
    {
        public List<CustomPin> myPins { get; set; }
        public List<CustomPin> MyPins { get => myPins; set { myPins = value; OnPropertyChanged("MyPins"); } }
        public ICommand CallPlace { get; set; }

        double? layoutHeight;
        double layoutBoundsHeight;
        int direction;
        const double layoutPropHeightMax = 0.75;
        const double layoutPropHeightMin = 0.04;
        void PanGestureHandler(object sender, PanUpdatedEventArgs e)
        {
            layoutHeight = layoutHeight ?? ((sender as StackLayout).Parent as AbsoluteLayout).Height;
            switch (e.StatusType)
            {
                case GestureStatus.Started:
                    layoutBoundsHeight = AbsoluteLayout.GetLayoutBounds(sender as StackLayout).Height;
                    break;
                case GestureStatus.Running:
                    direction = e.TotalY < 0 ? 1 : -1;
                    break;
                case GestureStatus.Completed:
                    if (direction > 0) // snap to max/min, you could use an animation....
                    {
                        AbsoluteLayout.SetLayoutBounds(bottomDrawer, new Rectangle(0.5, 1.00, 0.9, layoutPropHeightMax));
                      //  swipeLabel.Text = "Swipe me down";
                    }
                    else
                    {
                        AbsoluteLayout.SetLayoutBounds(bottomDrawer, new Rectangle(0.5, 1.00, 0.9, layoutPropHeightMin));
                       // swipeLabel.Text = "Swipe me up";
                    }
                    break;
            }
        }
        public PlacesList()
        {
            InitializeComponent();
            BindingContext =  this;

【问题讨论】:

  • Scrollview和CollectionView都可以实现contentview的上下移动功能,不需要在contentView上使用PanGestureRecognizer。告诉我们您想用更详细的信息做什么,然后我们可以提供更好的帮助。
  • 我想在用户向下切换时隐藏内容视图,并在用户向上切换时在页面上显示内容视图
  • 要隐藏/显示一个contentView,可以在swap down/down事件中将contentView的IsVisible属性设置为false/true。
  • 你能给我举个例子吗?我是关于如何写这个事件
  • 你想要draganddropgesture吗?你想移动控件还是滑动页面来隐藏视图?

标签: c# xaml xamarin xamarin.forms xamarin.android


【解决方案1】:

在视图上滑动时,您应该使用swipe gesture recognizer 来隐藏/显示内容视图:

<ContentPage.Content>

    <StackLayout BackgroundColor="LightYellow">
        <!-- Place new controls here -->

        <StackLayout.GestureRecognizers>

            <SwipeGestureRecognizer Direction="Down" Swiped="SwipeGestureRecognizer_Swiped"/>
            <SwipeGestureRecognizer Direction="Up" Swiped="SwipeGestureRecognizer_Swiped"/>

        </StackLayout.GestureRecognizers>

        <Label x:Name="myLabel" Text="Swipe down to hide me, swipe up to show me" BackgroundColor="Green"
       HorizontalOptions="Center"
       VerticalOptions="CenterAndExpand" />

    </StackLayout>
    
</ContentPage.Content>

在后面的代码中:

private void SwipeGestureRecognizer_Swiped(object sender, SwipedEventArgs e)
{
    switch (e.Direction)
    {
        case SwipeDirection.Left:
            // Handle the swipe
            break;
        case SwipeDirection.Right:
            // Handle the swipe
            break;
        case SwipeDirection.Up:
            // Handle the swipe
            myLabel.IsVisible = true;
            break;
        case SwipeDirection.Down:
            // Handle the swipe
            myLabel.IsVisible = false;
            break;
    }
}

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-10
  • 1970-01-01
相关资源
最近更新 更多