博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
How to detect when a list is scrolling (or not)
阅读量:5099 次
发布时间:2019-06-13

本文共 3629 字,大约阅读时间需要 12 分钟。

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

原文地址:

转载于:https://www.cnblogs.com/hebeiDGL/archive/2011/11/21/2257130.html

你可能感兴趣的文章
第23月第24天 git命令 .git-credentials git rm --cached git stash clear
查看>>
java SE :标准输入/输出
查看>>
一些方便系统诊断的bash函数
查看>>
【转载】基于vw等viewport视区相对单位的响应式排版和布局
查看>>
<转>关于MFC的多线程类 CSemaphore,CMutex,CCriticalSection,CEvent
查看>>
jquery中ajax返回值无法传递到上层函数
查看>>
css3之transform-origin
查看>>
[转]JavaScript快速检测浏览器对CSS3特性的支持
查看>>
Master选举原理
查看>>
[ JAVA编程 ] double类型计算精度丢失问题及解决方法
查看>>
小别离
查看>>
微信小程序-发起 HTTPS 请求
查看>>
WPF动画设置1(转)
查看>>
backgound-attachment属性学习
查看>>
个人作业——关于K米的产品案例分析
查看>>
基于node/mongo的App Docker化测试环境搭建
查看>>
java web 中base64传输的坑
查看>>
java 中的线程(一)
查看>>
秒杀9种排序算法(JavaScript版)
查看>>
素数判断BFS之“Prime Path”
查看>>