设为首页 收藏本站
查看: 493|回复: 0

Windows Phone 7 如何实现高效率的横向排版ListBox

[复制链接]

尚未签到

发表于 2015-5-9 09:09:22 | 显示全部楼层 |阅读模式
  使用ListBox进行数据绑定的时候默认都是竖向的排版方式,意思就是一个Item会占用一行的位置,竖向地并排下去。当我们使用ListBox时,使用横向排版的时候该怎么办呢?也就是说要在一行的位置上放两个或者两个以上的Item。通常的解决方法,我们会使用toolkit控件里面的WrapPanel排版。
  例如:



            











  通过设置ListBox的ItemsPanel属性的模板为WrapPanel控件的排版方式就会自动地根据Item的宽度大小并排地排在一行上,当排满了一行的时候就会继续排列在下面的一行上,如此类推不断地排下去,就实现了横向的数据绑定排版。但是这种方式有一个很致命的性能缺陷,因为会一次性地把所有的Item都初始化完成并展现在UI上,当Item的数量很多的时候就需要耗费很长的响应时间,导致用户体验很差,也会影响程序的性能。
  下面使用一种新的方法来解决WrapPanel横向排版引发的性能问题。
  当我们使用ListBox默认的排版方式绑定数据时,它不会一次性地将所有的Item全部初始化完毕并展示在UI上,它会根据屏幕的位置初始化部分的Item,这部分Item是在你看到的屏幕上的Item和屏幕上下一屏的Item。那就利用这种原理来设计一个横向排版的ListBox数据绑定。
  实现的方法是先将Item进行分组,一行要排列多少个Item那么就一组有多少个Item,分好组之后再把组作为一个新的Item构建一个新的数据绑定源。假如我们需要绑定的数据源的Item有200个,那么我们一行要排4个Item就要分50组,这时候构成的新的数据绑定源就是50行,在整体的ListBox里面是竖向排版,在一行的数据里面是横向排版,这就实现了跟WrapPanel的自动排版一样的绑定效果了,但是性能却比WrapPanel的自动排版要好很多。
  实例:
  Item.cs  数据源的Item



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Windows.Media;
using System.Threading;
namespace GridListDemo
{
public class Item : INotifyPropertyChanged
{
private string _name;
public string Name
{
get
{
return this._name;
}
set
{
if (this._name != value)
{
this._name = value;
RaisePropertyChanged("Name");
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChanged(string info)
{
PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
if (propertyChanged != null)
{
propertyChanged(this, new PropertyChangedEventArgs(info));
}
}
}
}
  GridDataRow.cs 组的数据源集合



using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
namespace GridListDemo
{
public class GridDataRow : IList, ICollection, IEnumerable, IEnumerable
{
private IList _items;//所有的item集合
private int _offset;//偏移量 即 前面的item数量
private int _rowItemCount;//行数
public GridDataRow(IList itemsList, int offset, int rowItemCount)
{
this._items = itemsList;
this._offset = offset;
this._rowItemCount = rowItemCount;
}
public void Add(T item)
{
throw new NotImplementedException();
}
public void Clear()
{
throw new NotImplementedException();
}
public bool Contains(T item)
{
throw new NotImplementedException();
}
public void CopyTo(T[] array, int arrayIndex)
{
throw new NotImplementedException();
}
public IEnumerator GetEnumerator()
{
throw new NotImplementedException();
}
public int IndexOf(T item)
{
throw new NotImplementedException();
}
public void Insert(int index, T item)
{
throw new NotImplementedException();
}
public bool Remove(T item)
{
throw new NotImplementedException();
}
public void RemoveAt(int index)
{
throw new NotImplementedException();
}
IEnumerator IEnumerable.GetEnumerator()
{
throw new NotImplementedException();
}
public int Count
{
get
{
//取行数和剩下的条数的最小的一个
int num = this._items.Count - this._offset;
return Math.Min(this._rowItemCount, num);
}
}
public bool IsReadOnly
{
get
{
return true;
}
}
public T this[int index]
{
get
{
return this._items[this._offset + index];
}
set
{
throw new NotImplementedException();
}
}
}
}
  RowCollection.cs 行的绑定数据源的集合



using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Reflection;
using System.Threading;
using System.Windows;
namespace GridListDemo
{
public class RowCollection : IList, IList where T : new()
{
private IList _itemsCollection;
private int _rowItemCount;//一行的数量
public RowCollection(IList itemsCollection, int rowItemCount)
{
this._itemsCollection = itemsCollection;
this._rowItemCount = rowItemCount;
}
public void Add(GridDataRow item)
{
throw new NotImplementedException();
}
public int Add(object value)
{
throw new NotImplementedException();
}
public void Clear()
{
throw new NotImplementedException();
}
public bool Contains(object value)
{
throw new NotImplementedException();
}
public bool Contains(GridDataRow item)
{
throw new NotImplementedException();
}
public void CopyTo(Array array, int index)
{
throw new NotImplementedException();
}
public void CopyTo(GridDataRow[] array, int arrayIndex)
{
throw new NotImplementedException();
}
public IEnumerator GetEnumerator()
{
throw new NotImplementedException();
}
public int IndexOf(object value)
{
return -1;
}
public int IndexOf(GridDataRow item)
{
return -1;
}
public void Insert(int index, GridDataRow item)
{
throw new NotImplementedException();
}
public void Insert(int index, object value)
{
throw new NotImplementedException();
}
public void Remove(object value)
{
throw new NotImplementedException();
}
public bool Remove(GridDataRow item)
{
throw new NotImplementedException();
}
public void RemoveAt(int index)
{
throw new NotImplementedException();
}
IEnumerator IEnumerable.GetEnumerator()
{
throw new NotImplementedException();
}
public int Count
{
get
{
//总数处于一行的数量等于列表的行数
return Convert.ToInt32(Math.Ceiling((double)(((double)this._itemsCollection.Count) / ((double)this._rowItemCount))));
}
}
public bool IsFixedSize
{
get
{
return false;
}
}
public bool IsReadOnly
{
get
{
throw new NotImplementedException();
}
}
public bool IsSynchronized
{
get
{
return false;
}
}
public GridDataRow this[int index]
{
get
{
return new GridDataRow(this._itemsCollection, index * this._rowItemCount, this._rowItemCount);
}
set
{
throw new NotImplementedException();
}
}
public object SyncRoot
{
get
{
return this;
}
}
object IList.this[int index]
{
get
{
return this[index];
}
set
{
throw new NotImplementedException();
}
}
}
}
  MyGridRow.cs 自定义的组控件



using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
namespace GridListDemo
{
///
/// 横向排版,继承Canvas控件
///
public class MyGridRow : Canvas
{
//定义ItemsSource属性
public static readonly DependencyProperty ItemsSourceProperty = DependencyProperty.Register("ItemsSource", typeof(IList), typeof(MyGridRow), new PropertyMetadata(new PropertyChangedCallback(MyGridRow.OnItemsSourceChanged)));
///
/// 初始化GridRow控件
///
private void ApplyRaw()
{
if ((this.ItemsSource == null) || (this.ItemsSource.Count != base.Children.Count))
{
base.Children.Clear();
if (this.ItemsSource != null)
{
for (int i = 0; i < this.ItemsSource.Count(); i++)
{
Item item = this.ItemsSource;
TextBlock tb = new TextBlock
{
DataContext = item,
Width = 80.0,
Height = 80.0
};
Binding binding = new Binding("Name")
{
FallbackValue = null
};
BindingOperations.SetBinding(tb, TextBlock.TextProperty, binding);
//添加目标到Canvas控件里面
base.Children.Add(tb);
Canvas.SetLeft(tb, (double)(i * 0x72));
}
}
}
else
{
for (int j = 0; j < this.ItemsSource.Count(); j++)
{
Item item2 = this.ItemsSource[j];
TextBlock tb2 = (TextBlock)base.Children[j];
tb2.Text = item2.Name;
}
}
}
///
/// ItemsSource改变事件
///
///
///
private static void OnItemsSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
(d as MyGridRow).ApplyRaw();
}
//ItemsSource属性
public IList ItemsSource
{
get
{
return (IList)base.GetValue(ItemsSourceProperty);
}
set
{
base.SetValue(ItemsSourceProperty, value);
}
}
}
}
  在页面中实现



   




.......



  List source = new List();
for (int i = 0; i < 200; i++)
{
source.Add(new Item { Name = "name" + i });
}
this.GridItemsListBox.ItemsSource = new RowCollection(source, 4);
  运行的效果
DSC0000.png
  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-65140-1-1.html 上篇帖子: [Windows Phone] Windows Phone 7 播放远程流媒体的代码实现方法 下篇帖子: 转载:Windows Phone 7 资源汇总(超全)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表