我通过利用 ScrollViewer 的 VisualStateGroup 解决了这个问题。
// uie is a UIElement
IEnumerable<ScrollViewer> svList = uie.GetVisualDescendants<ScrollViewer>();
if (svList.Count() > 0)
{
// Visual States are always on the first child of the control
FrameworkElement element = VisualTreeHelper.GetChild(svList.First<ScrollViewer>(), 0) as FrameworkElement;
// getting all the visual state groups
IList groups = VisualStateManager.GetVisualStateGroups(element);
foreach (VisualStateGroup group in groups)
{
if (group.Name == "ScrollStates")
{
group.CurrentStateChanged += new EventHandler<VisualStateChangedEventArgs>(group_CurrentStateChanged);
}
}
}
private static void group_CurrentStateChanged(object sender, VisualStateChangedEventArgs e)
{
if (e.NewState.Name == "NotScrolling")
{
isNotScrolling = true;
}
else
{
isNotScrolling = false;
}
}
我遇到的唯一问题是 ScrollStates 的状态变化非常缓慢,因此应用程序触发的其他事件将在您的代码甚至注册滚动之前处理。