zoukankan      html  css  js  c++  java
  • How to detect when a list is scrolling (or not)

    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

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

  • 相关阅读:
    mybatis3这个问题我晕为什么对于配置信息的节点放的位置也会报错
    QTP的那些事增删改查中的增加操作的测试用例及其脚本设计思路
    QTP的那些事importsheet注意的一些事情
    mybatis3中的结果集
    QTP的那些事终极项目脚本设计思路及其测试查询功能的一些实际项目体会
    mybatis+spring整合的几个好的例子
    QTP的那些事操作excel数据需要注意的事
    hibernate4的使用第一步环境搭建
    项目中关于IFRAME引发的问题【出现率很高】
    oracle直接sql语句后台递归查询返回一个树
  • 原文地址:https://www.cnblogs.com/hebeiDGL/p/2257130.html
Copyright © 2011-2022 走看看