How can I detect when scrolling has started in a L

2019-04-17 10:42发布

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条回答
狗以群分
2楼-- · 2019-04-17 11:45

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;
});
查看更多
登录 后发表回答