代码之家  ›  专栏  ›  技术社区  ›  DarkW1nter

Xamarin表单-检测用户何时开始在滚动视图中向下滚动

  •  0
  • DarkW1nter  · 技术社区  · 7 年前

    我有一个scrollview,当用户开始向上滚动时,我会运行一些代码(onswipup())。我使用

    textScroll.Scrolled += (sender, e) => { onScrolled(); };
    
    private void onScrolled()
    {
          if(textScroll.ScrollY > 0)
          {
              OnSwipeUp(null, null);
          }
    }
    

    我还需要检测用户何时开始向下滚动,所以我想得到scrollview的最大Y位置,然后说

    if(textScroll.ScrollY < maxY)
    

    我能做到吗?

    0 回复  |  直到 7 年前
        1
  •  4
  •   Himanshu Dwivedi    7 年前

    试试这个:

    private double previousScrollPosition = 0;
    void Handle_Scrolled(object sender, Xamarin.Forms.ScrolledEventArgs e) 
    {
    
      if (previousScrollPosition < e.ScrollY) 
      {
        //scrolled down
        previousScrollPosition = e.ScrollY;
      } 
      else 
      {
          //scrolled up
    
        if (Convert.ToInt16(e.ScrollY) == 0)
        previousScrollPosition = 0;
    
      }
    }
    
        2
  •  1
  •   AbbyWang - MSFT    7 年前

    您不需要获取最大Y位置,因为在向下滚动之前,Y位置始终为0。所以你只需要知道滚动的方向。 我写了一个演示来证明这一点:

    主页。xaml。反恐精英

    private double previousScrollPosition = 0;
    
            public MainPage()
            {
                InitializeComponent();
            }
    
            void Handle_Scrolled(object sender, Xamarin.Forms.ScrolledEventArgs e)
            {
                if (previousScrollPosition < e.ScrollY)
                {
                    //scrolled down
                    Console.WriteLine("!!!!!!!!!scrolled down   ScrollY=>" + textScroll.ScrollY);
                }
                else
                {
                    //scrolled up
                    Console.WriteLine("!!!!!!!!!scrolled up   ScrollY=>" + textScroll.ScrollY);
                }
                previousScrollPosition = e.ScrollY;
            }
    

    主页。xaml

    <ContentPage.Content>
            <StackLayout>
                <BoxView BackgroundColor="Red" HeightRequest="200" WidthRequest="150" />
                <ScrollView x:Name="textScroll" Scrolled="Handle_Scrolled">
                    <StackLayout>
                        <Entry Text="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"/>
                        <Entry Text="bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"/>
                        <Entry Text="ccccccccccccccccccccccccccccccccccccccccccccccccccc"/>
                        <Entry Text="ddddddddddddddddddddddddddddddddddddddddddddddddddddddddd"/>
                        <Entry Text="eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee"/>
                        <Entry Text="fffffffffffffffffffffffffffffffffffffffffff"/>
                        <Entry Text="ggggggggggggggggggggggggggggggggggggggggggggg"/>
                        <Entry Text="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"/>
                        <Entry Text="bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"/>
                        <Entry Text="ccccccccccccccccccccccccccccccccccccccccccccc"/>
                        <Entry Text="dddddddddddddddddddddddd"/>
                        <Entry Text="eeeeeeeeeeeeeeeeeeeeeeeeee"/>
                        <Entry Text="fffffffffffffffffffffffffffffffffffffffffff"/>
                        <Entry Text="ggggggggggggggggggggggggggggggggggggggggggggg"/>
                        <Entry Text="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"/>
                        <Entry Text="bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"/>
                        <Entry Text="ccccccccccccccccccccccccccccccccccccccccccccccccccc"/>
                        <Entry Text="ddddddddddddddddddddddddddddddddddddddddddddddddddddddddd"/>
                        <Entry Text="eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee"/>
                        <Entry Text="fffffffffffffffffffffffffffffffffffffffffff"/>
                        <Entry Text="ggggggggggggggggggggggggggggggggggggggggggggg"/>
                        <Entry Text="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"/>
                        <Entry Text="bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"/>
                        <Entry Text="ccccccccccccccccccccccccccccccccccccccccccccc"/>
                        <Entry Text="dddddddddddddddddddddddd"/>
                        <Entry Text="eeeeeeeeeeeeeeeeeeeeeeeeee"/>
                        <Entry Text="fffffffffffffffffffffffffffffffffffffffffff"/>
                        <Entry Text="ggggggggggggggggggggggggggggggggggggggggggggg"/>
                    </StackLayout>
                </ScrollView>
    
            </StackLayout>
    
        </ContentPage.Content>
    
    推荐文章