WP8_检测列表是否滑动

本文详细介绍了一种在Windows Phone 8中检测列表是否正在滚动的方法。通过利用Silverlight的视觉状态组,可以监听列表从滚动到停止滚动的状态变化,从而实现更精细的UI控制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

posted on 2014-09-04 00:45  v.e.n.u.s 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/jx270/p/3955140.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值