How can I detect when scrolling has started in a ListView on Windows 10 UWP?

一个人想着一个人 提交于 2019-12-06 17:09:29

问题


I'd like to subscribe to an event which tells me that scrolling has started in a ListView and get the direction of scrolling.

Is there any way to do this in Windows 10 UWP API?

Thanks


回答1:


You should first obtain the ScrollViewer inside the ListView and then subscribe to its DirectManipulationStarted event.

However, to get the direction of the scrolling can be tricky. I'd suggest you to have a look at the new Windows Composition API where there's a way to use ExpressionAnimation to link the ScrollViewer's translation to a value of your choice.

A good start is to have a look at this demo from James Clarke.

private void MainPage_Loaded(object sender, RoutedEventArgs e)
{
    CompositionPropertySet scrollerViewerManipulation = ElementCompositionPreview.GetScrollViewerManipulationPropertySet(myScroller);

    Compositor compositor = scrollerViewerManipulation.Compositor;

    ExpressionAnimation expression = compositor.CreateExpressionAnimation("ScrollManipululation.Translation.Y * ParallaxMultiplier");

    expression.SetScalarParameter("ParallaxMultiplier", 0.3f);
    expression.SetReferenceParameter("ScrollManipululation", scrollerViewerManipulation);

    Visual textVisual = ElementCompositionPreview.GetElementVisual(background);
    textVisual.StartAnimation("Offset.Y", expression);
}

Update

Actually just thought of an easier way to detect the scrolling direction. Just subscribe whenever the VerticalOffset is changed and compare it to its previous values.

private double _previousOffset;  

sv.RegisterPropertyChangedCallback(ScrollViewer.VerticalOffsetProperty, (s, dp) =>
{
    if (Math.Abs(sv.VerticalOffset - _previousOffset ) < 3)
    {
        // ignore when offset difference is too small
    }
    else if (sv.VerticalOffset > _previousOffset)
    {
        Debug.WriteLine($"up {sv.VerticalOffset - _previousOffset}");
    }
    else {
        Debug.WriteLine($"down {sv.VerticalOffset - _previousOffset}");
    }

    _previousOffset = sv.VerticalOffset;
});


来源:https://stackoverflow.com/questions/35049071/how-can-i-detect-when-scrolling-has-started-in-a-listview-on-windows-10-uwp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!