如何检测 Windows 10 UWP 上的 ListView 何时开始滚动? [英] How can I detect when scrolling has started in a ListView on Windows 10 UWP?

查看:22
本文介绍了如何检测 Windows 10 UWP 上的 ListView 何时开始滚动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想订阅一个事件,该事件告诉我在 ListView 中已经开始滚动并获取滚动方向.

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

有没有办法在 Windows 10 UWP API 中做到这一点?

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

谢谢

推荐答案

你应该先获取 ListView 中的 ScrollViewer 然后订阅它的 DirectManipulationStarted 事件.

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

然而,要获得滚动的方向可能很棘手.我建议您查看新的 Windows Composition API,其中有一种方法可以使用 ExpressionAnimationScrollViewer 的翻译链接到您选择的值.

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);
}

更新

其实只是想到了一个更简单的方法来检测滚动方向.只需在 VerticalOffset 更改时订阅,并将其与之前的值进行比较.

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;
});

这篇关于如何检测 Windows 10 UWP 上的 ListView 何时开始滚动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆