One of the UI features of lists on Windows Phone 7 is that the "scroll bars" don't really act like traditional scroll bars; they are non-interactive and they only appear when the list is actually scrolling. To achieve this, the Silverlight team added a new visual state group that is used in the default control template for showing / hiding the scroll bars.

You can get programmatic access to the scroll state with the following approach. First, paste this XAML into the ContentGrid of a new WP7 application:

<ListBox FontSize="50" x:Name="theList">
  <ListBoxItem Content="Item 00"/>
  <ListBoxItem Content="Item 01"/>
  <ListBoxItem Content="Item 02"/>
  <ListBoxItem Content="Item 03"/>
  <ListBoxItem Content="Item 04"/>
  <ListBoxItem Content="Item 05"/>
  <ListBoxItem Content="Item 06"/>
  <ListBoxItem Content="Item 07"/>
  <ListBoxItem Content="Item 08"/>
  <ListBoxItem Content="Item 09"/>
  <ListBoxItem Content="Item 10"/>
  <ListBoxItem Content="Item 11"/>
  <ListBoxItem Content="Item 12"/>
  <ListBoxItem Content="Item 13"/>
  <ListBoxItem Content="Item 14"/>
  <ListBoxItem Content="Item 15"/>
  <ListBoxItem Content="Item 16"/>
  <ListBoxItem Content="Item 17"/>
  <ListBoxItem Content="Item 18"/>
  <ListBoxItem Content="Item 19"/>
  <ListBoxItem Content="Item 20"/>
  <ListBoxItem Content="Item 21"/>
  <ListBoxItem Content="Item 22"/>
  <ListBoxItem Content="Item 23"/>
  <ListBoxItem Content="Item 24"/>
  <ListBoxItem Content="Item 25"/>
  <ListBoxItem Content="Item 26"/>
  <ListBoxItem Content="Item 27"/>
  <ListBoxItem Content="Item 28"/>
  <ListBoxItem Content="Item 29"/>
</ListBox>

Now add the following to the code-behind file:

public MainPage()
{
  InitializeComponent();
  Loaded += new RoutedEventHandler(MainPage_Loaded);
}

bool alreadyHookedScrollEvents = false;

void MainPage_Loaded(object sender, RoutedEventArgs e)
{
  if (alreadyHookedScrollEvents)
    return;

alreadyHookedScrollEvents = true;
  ScrollViewer viewer = FindSimpleVisualChild<ScrollViewer>(theList);
  if (viewer != null)
  {
    // Visual States are always on the first child of the control template
    FrameworkElement element = VisualTreeHelper.GetChild(viewer, 0) as FrameworkElement;
    if (element != null)
    {
      VisualStateGroup group = FindVisualState(element, "ScrollStates");
      if (group != null)
      {
        group.CurrentStateChanging += (s, args) => PageTitle.Text = args.NewState.Name;
      }
    }
  }
}

VisualStateGroup FindVisualState(FrameworkElement element, string name)
{
  if (element == null)
    return null;

IList groups = VisualStateManager.GetVisualStateGroups(element);
  foreach (VisualStateGroup group in groups)
    if (group.Name == name)
      return group;

return null;
}

T FindSimpleVisualChild<T>(DependencyObject element) where T : class
{
  while (element != null)
  {

if (element is T)
      return element as T;

element = VisualTreeHelper.GetChild(element, 0);
  }

return null;
}

What this does is attach to the CurrentStateChanging event of the VisualStateGroup, which will be raised every time the scroll state changes from "Scrolling" to "NotScrolling" or vice-versa. There's a bunch of infrastructure code to walk the visual tree and pull out a state group, but the core code is very simple:

  1. First we attach a handler called MainPage_Loaded to the Page.Loaded event
  2. When the page loads, we call FindSimpleVisualChild to get the ScrollViewerchild of the list (this is a pretty dumb function)
    1. We make sure to only do this once, because the page could get loaded more than once if it is navigated
  3. Then we call FindVisualState to get the named visual state from the first child of the ScrollViewer
  4. Then we add a handler to the CurrentStateChanging event

原文作者:Peter Torr - MSFT

原文链接: http://blogs.msdn.com/b/ptorr/archive/2010/07/23/how-to-detect-when-a-list-is-scrolling-or-not.aspx

WP8_检测列表是否滑动的更多相关文章

  1. android中列表的滑动删除仿ios滑动删除

    大家是不是觉得ios列表的滑动删除效果很酷炫?不用羡慕android也可以实现相同的效果 并且可以自定义效果,比如左滑删除,置顶,收藏,分享等等 其实就是自定义listview重写listview方法 ...

  2. Android-自定义仿QQ列表Item滑动

    效果图: 布局中去指定自定义FrameLayout: <!-- 自定义仿QQ列表Item滑动 --> <view.custom.shangguigucustomview.MyCust ...

  3. python极简代码之检测列表是否有重复元素

    极简python代码收集,实战小项目,不断撸码,以防遗忘.持续更新: 1,检测列表是否有重复元素: 1 # !usr/bin/env python3 2 # *-* coding=utf-8 *-* ...

  4. 提升html5的性能体验系列之二列表流畅滑动

    App的顶部一般有titlebar,下面是list.常见的一个需求是要在list滚动时,titlebar不动.这个简单的需求,实现起来其实并不简单. 在普通web上的做法是使用div的滚动条,把lis ...

  5. [转]ionic3项目实战教程三(创建provider、http请求、图文列表、滑动列表)

    本文转自:https://blog.csdn.net/lyt_angularjs/article/details/81145468 版权声明:本文为博主原创文章,转载请注明出处.谢谢! https:/ ...

  6. 提升HTML5的性能体验系列之二 列表流畅滑动

    App的顶部一般有titlebar,下面是list.常见的一个需求是要在list滚动时,titlebar不动.这个简单的需求,实现起来其实并不简单. 在普通web上的做法是使用div的滚动条,把lis ...

  7. 微信小程序列表项滑动显示删除按钮

    微信小程序并没有提供列表控件,所以也没有iOS上惯用的列表项左滑删除的功能,SO只能自己干了. 原理很简单,用2个层,上面的层显示正常的内容,下面的层显示一个删除按钮,就是记录手指滑动的距离,动态的来 ...

  8. 模仿qq列表信息滑动删除效果

    这个效果的完成主要分为两个部分 自定义view作为listview的列表项 一个view里面包括 显示头像,名字,消息内容等的contentView和滑动才能显示出来的删除,置顶的右边菜单menuVi ...

  9. egret之好友列表(滑动列表)

    本文采用List+Scroller实现列表滑动功能 首先新建两个皮肤,一个用做好友界面的显示,一个用作单个好友的显示,新建皮肤如下: 皮肤一取名为:wxMainSkin,添加如下控件 皮肤一取名为:w ...

随机推荐

  1. DBA_Oracle Erp中某个Form需进行升级Patch详解(案例)

    2014-06-21 Created By BaoXinjian

  2. OAF_EO系列4 - Create详解和实现(案例)

    2014-06-02 Created By BaoXinjian

  3. C# WinForm开发系列 - ListBox/ListView/Panel

    转自会飞的小猪文章 C# WinForm开发系列 - ListBox/ListView/Panel 在博客园看到了一篇博文,觉得很不错,就转载过来了.    包含自定义绘制的ListBox, 带拖动, ...

  4. JAVA final关键字,常量的定义

    final(最终)是一个修饰符1.final可以修饰类,函数,变量(成员变量,局部变量)2.被final修饰后的类不可以被其它类继承3.被final修饰后的方法(函数)不可以被重写4.被final修饰 ...

  5. 监控和管理Cassandra

    了解Cassandra集群的性能特点有助于诊断和维护Cassandra.由于Cassandra使用JAVA开发的,所以它就提供了JMX环境下的一些管理工具来管理Cassandra,它们包括:Cassa ...

  6. Java多线程之新类库中的构件CountDownLatch

    使用CountDownLatch类 一个同步辅助类,在完成一组正在其他线程中执行的操作之前,它允许一个或多个线程一直等待. 主要方法 public CountDownLatch(int count); ...

  7. 庭审精彩语录整理 z

    公诉人:用百度搜索淫秽关键字+快播,搜索结果得出超过4200万结果,可见快播在传播淫秽视频方面的巨大影响.王欣:这个没有任何意义,您可以用百度搜索淫秽关键字+QQ看有多少结果. 新浪科技讯 1月8日下 ...

  8. ubuntu 命令学习大全

    http://wiki.ubuntu.org.cn/UbuntuSkills#.E6.98.BE.E7.A4.BA.E5.BD.93.E5.89.8D.E7.A1.AC.E4.BB.B6.E4.BF. ...

  9. centos7安装mysql

    centos7安装mysql 1 查找系统是否安装了myql rpm -q mysql mysql-server1.1如果安装了.就删除 sudo yum -y remove mysql mysql- ...

  10. go mobile 得生命周期事件

    生命周期事件,就是状态从一个阶段切换成另外一个状态时触发的事件.所以我们可以看到 lifecycle.Event 的定义如下:   生命周期一共有下面四个阶段: lifecycle.StageDead ...