jacky4955 发表于 2015-5-12 10:56:45

Windows Phone 7 ListBox控件滚动分页加载数据

  using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Controls.Primitives;
namespace SilverlightApplication1
{
    public class ListBoxScrollPage
    {
      private static volatile ListBoxScrollPage instance;
      private static object syncObj = new object();
      public ListBoxScrollPage()
      {
      }
      public static ListBoxScrollPage Instance
      {
            get
            {
                if (null == instance)
                {
                  lock (syncObj)
                  {
                        if (null == instance)
                        {
                            instance = new ListBoxScrollPage();
                        }
                  }
                }
                return instance;
            }
      }
      ///
      /// 使用视图树根据给定的子元素类型,从对象中查找子元素
      ///
      ///
      ///
      ///
      private T FindChildOfType(DependencyObject element)
      where T : class
      {
            if (null != element)
            {
                if (element is T)
                  return (element as T);
                for (int i = 0; i < VisualTreeHelper.GetChildrenCount(element); i++)
                {
                  DependencyObject child = VisualTreeHelper.GetChild(element, i);
                  T grandChild = FindChildOfType(child);
                  if (null != grandChild)
                        return grandChild;
                }
            }
            return null;
      }
      ///
      /// 根据ScrollBar分页
      ///
      ///
      ///
      public void PageByScroll(ListBox current, RoutedPropertyChangedEventHandler scrollBar_ValueChanged)
      {
            ScrollBar scrollBar = FindChildOfType(current);
            if (null != scrollBar && scrollBar.Orientation != System.Windows.Controls.Orientation.Horizontal)
            {
                scrollBar.ValueChanged += scrollBar_ValueChanged;
            }
      }

      //页面调用
      //void MainPage_Loaded(object sender, RoutedEventArgs e)
      //{
      //    ListBoxScrollPage.PageByScroll(ListBox, (obj, args) =>
      //    {
      //      ScrollBar scrollBar = sender as ScrollBar;
      //      object valueObj = scrollBar.GetValue(ScrollBar.ValueProperty);
      //      object maxObj = scrollBar.GetValue(ScrollBar.MinimumProperty);
      //      if (null != valueObj && maxObj != null)
      //      {
      //            double value = (double)valueObj;
      //            double max = (double)maxObj - 1.0;
      //            if (value >= max)
      //            {
      #region 读取下一页的数据
      #endregion
      //            }
      //      }
      //    });
      //}
      //
      ///
      /// 根据ScrollViewer分页
      ///
      ///
      ///
      //void lbxItem_LayoutUpdated(object sender, EventArgs e)
      //{
      //    ScrollViewer scrollViewer = FindChildOfType(ListBox);//ScrollViewerscrollBar
      //    if (scrollViewer == null)
      //    {
      //      throw new InvalidOperationException("erro");
      //    }
      //    else
      //    {      //判断当前滚动的高度是否大于或者等于scrollViewer实际可滚动高度,如果等于或者大于就证明到底了
      //      if (scrollViewer.VerticalOffset >= scrollViewer.ScrollableHeight)
      //      {
      //            #region 读取下一页的数据
      //            #endregion
      //      }
      //    }
      //}
    }
}
页: [1]
查看完整版本: Windows Phone 7 ListBox控件滚动分页加载数据