windows phone listbox虚拟化(下)
之前写过一篇关于listbox虚拟化的文章,那里采用的方法都是自己早期研究的一些思路,然后发现当数据很大的时候,其实性能效果还是不太理想,下面让我们来仔细想一想到底是基于什么原因,我们回去破坏默认的虚拟化呢?首先一个原因就是下来刷新的问题:由于listbox默认是不支持刷新效果的,这个时候我们可能回去重新自定义listbox,但是我们一旦重写listbox就有可能带来虚拟化的问题,针对这一问题,现在比较好的解决方法就是:乱世经典博客,这种方法并不是像现在很多网路通用的listbox刷新,重写三个自定义控件,它里面只是通过切换不同的VisualStateGroup,这样的话listbox的itemplans就还是支持虚拟化的面板。既然刷新的问题,我们有很好的办法解决虚拟化。那么还有其他的原因会让我们破坏虚拟化吗?肯定是有的,就是当整个页面列表的布局很复杂时候,这种情况下我们可能需要在listbox里面放置自定义控件,一旦你放置usercontrol的话,虚拟化就被破坏了,比如有的时候,我们可能需要listbox里面嵌套listbox这个时候虚拟化肯定是不行的,其实这种情况也有一种很好的解决办法就是:阿干@.NET的博客,在这里面我们可以看到针对listbox里面嵌套listbox的情况,我们可以使用itemcontrols,这样一来的话,它的itempanels就还是虚拟化面板,因此虚拟化还是没有被破坏掉的。
其实还有一种变态的情况就是有的时候我们需要listbox显示的数据是横排的,通常我们的做法就是:
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<toolkit:WrapPanel></toolkit:WrapPanel>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
这样一来的话,就必然会导致虚拟化被破坏,因为你连面板都换掉了。那么针对这种情况有没有好的解决办法呢?其实阿干那篇博客里面就提供了好的解决办法,我们可以自己去实现分列显示的效果,核心的代码是:
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.Collections.Generic;
using System.Collections.Specialized;
using System.Diagnostics; namespace MoHoo.MultiColumnListBox.Demo
{
public class RowAdapter<TItemType> : IList<IEnumerable<TItemType>>, INotifyCollectionChanged
{
private readonly IList<TItemType> _sourceList;
private readonly int _columns; public IList<TItemType> SourceList
{
get { return _sourceList; }
} private class RowObject : IEnumerable<TItemType>
{
internal readonly RowAdapter<TItemType> Parent;
internal readonly int StartIndex; public RowObject(RowAdapter<TItemType> parent, int startIndex)
{
Parent = parent;
StartIndex = startIndex;
} #region IEnumerable<TItemType> Members public IEnumerator<TItemType> GetEnumerator()
{
int limit = Parent._sourceList.Count;
int end = Math.Min(StartIndex + Parent._columns, limit); for (int pos = StartIndex; pos < end; ++pos)
{
yield return Parent._sourceList[pos];
}
} #endregion #region IEnumerable Members System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return GetEnumerator();
} #endregion
} public RowAdapter(IList<TItemType> sourceList, int columns)
{
if (null == sourceList)
throw new ArgumentNullException("sourceList", "Resource.RowAdapter_RowAdapter_sourceList_is_null");
if (columns <= )
throw new ArgumentOutOfRangeException("columns", "Resource.RowAdapter_RowAdapter_ColumnsGreaterOne"); // We require the source list to implement IList because we
// need to know how many item there are
_sourceList = sourceList;
_columns = columns; var sourceNotify = sourceList as INotifyCollectionChanged;
if (null != sourceNotify)
{
sourceNotify.CollectionChanged += OnSourceCollectionChanged;
}
} #region IList<IEnumerable<TItemType>> Members public int IndexOf(IEnumerable<TItemType> item)
{
var realItem = item as RowObject;
if (null == realItem || !ReferenceEquals(realItem.Parent, this))
return -; // It does not belong to this collection Debug.Assert( == realItem.StartIndex % _columns, "RowObject item has a wierd index");
return realItem.StartIndex / _columns;
} public void Insert(int index, IEnumerable<TItemType> item) {
throw new NotSupportedException();
} public IEnumerable<TItemType> this[int index]
{
get
{
if (index < || index > Count)
return null; return InternalGetRow(index);
}
set
{
throw new NotSupportedException();
}
} public void RemoveAt(int index)
{
throw new NotSupportedException();
} #endregion #region ICollection<IEnumerable<TItemType>> Members public void Add(IEnumerable<TItemType> item)
{
throw new NotSupportedException();
} public bool Contains(IEnumerable<TItemType> item)
{
var realItem = item as RowObject;
return null != realItem && object.ReferenceEquals(realItem.Parent, this);
} public void CopyTo(IEnumerable<TItemType>[] array, int arrayIndex)
{
// I haven't implemented this. It is easy to implement if you need it
throw new NotImplementedException();
} public bool Remove(IEnumerable<TItemType> item)
{
throw new NotSupportedException();
}
public void Clear()
{
throw new NotSupportedException();
} public int Count
{
get
{
return (_sourceList.Count + (_columns - )) / _columns;
}
} public bool IsReadOnly
{
get { return true; }
} #endregion #region IEnumerable<IEnumerable<TItemType>> Members public IEnumerator<IEnumerable<TItemType>> GetEnumerator()
{
for (int i = ; i < Count; ++i)
{
yield return InternalGetRow(i);
}
} #endregion #region IEnumerable Members System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return GetEnumerator();
} #endregion #region INotifyCollectionChanged Members public event NotifyCollectionChangedEventHandler CollectionChanged; private void FireCollectionChanged()
{ var handler = CollectionChanged;
if (null != handler)
{
handler(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
}
} private void OnSourceCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
FireCollectionChanged();
} #endregion private RowObject InternalGetRow(int index)
{
return new RowObject(this, index * _columns);
}
}
}
这里面的核心思想就是自定义一个索引器。
windows phone listbox虚拟化(下)的更多相关文章
- Windows Phone Listbox虚拟化的问题
最近在项目里面碰到最头疼的一个难题就是Listbox虚拟化的问题,查阅很多文档其实Listbox本身是支持虚拟化的,那么在什么情况下会破坏Listbox的虚拟化呢?目前我接触到的主要有两个原因:一.是 ...
- Windows phone UI虚拟化和数据虚拟化(二)
书接上回的Windows phone UI虚拟化和数据虚拟化(一)我们学习了wp的ui虚拟化.今天来和大家分享一下wp的数据虚拟化. 并同时感谢我的同事dgwutao在编写此文时给我的巨大帮助,3ks ...
- Windows phone UI虚拟化和数据虚拟化(一)
今天和大家分享一些关于windows phone ui虚拟化和数据虚拟化的一些知识. 也顺便回答我上一篇[LongListSelector 控件 在 wp7 和wp8中的不同之处]里,留下的那个问题, ...
- Windows Server 2012 虚拟化实战:网络(二)
关于Windows Server的虚拟化网络,前文描述了在操作系统层面上的出现的配置变化.其中的一些配置通过Windows Server提供的小工具即可实现,如网卡组的配置,而有些需要安装Window ...
- Windows Server 2012 虚拟化实战:存储(二)
五.搭建Window Server 2012虚拟化的存储网络 前文我们讨论了Window Server 2012支持的各种与存储相关的技术,接下来我们通过实践对其中的一些技术进行检验.实际上Windo ...
- Windows Server 2012 虚拟化实战:存储(一)
在计算机世界我们随处可以见的一种方法,那就是抽象.1946年冯诺依曼提出了计算机的基本结构包含:计算器,存储器和I/O设备.这可能是对计算机这一新生事物最重要的一次抽象,它直接影响了今后几十年计算机软 ...
- Windows Server 2003 Sp2 下无法安装SQL Server 2008 Management Studio Express问题
Windows Server 2003 Sp2 下无法安装SQL Server 2008 Management Studio Express问题钉子 发表于 2010-5-22 1:42:51问题描述 ...
- Windows及Linux平台下的计时函数总结
本文对Windows及Linux平台下常用的计时函数进行总结,包括精度为秒.毫秒.微秒三种精度的各种函数.比如Window平台下特有的Windows API函数GetTickCount().timeG ...
- Thrift在Windows及Linux平台下的安装和使用示例
本文章也同时发表在个人博客Thrift在Windows及Linux平台下的安装和使用示例上. thrift介绍 Apache Thrift 是 Facebook 实现的一种高效的.支持多种编程语言的R ...
随机推荐
- 【PL/SQL练习】DML语句的处理(可以处理多行数据)
1.Insert (在表中插入一行数据,并查看) SQL> desc t1; Name Type Nullable Default Comments ---- ------------ ---- ...
- 不能读取文件“itunes.library.itl”因为它是由更高级别的itunes所创建的
转自:https://zhidao.baidu.com/question/80796363.html 是因为你安装过高版本的后又装你版本的itunes. 你在电脑上搜索所有硬盘上的itunes lib ...
- AJAX(学习笔记一)
1:什么是AJAX? AJAX是一组英文单词的简写,这组英文单词是 :Asynchronous JavaScript and XML ,翻译成中文的意思是: 异步的JavaScript 和 XML.什 ...
- 完成《Java编程入门》初稿
Java编程入门 现在的运维工程师不但要懂得集合网络.系统管理而且要和开发人员一起调试系统,社会上也需要"复合性"的运维人员,所以需要做运维的也要懂一些开发,知道软件系统接口的调试 ...
- Windows API学习---插入DLL和挂接API
插入DLL和挂接API 在Microsoft Windows中,每个进程都有它自己的私有地址空间.当使用指针来引用内存时,指针的值将引用你自己进程的地址空间中的一个内存地址.你的进程不能创建一个其引用 ...
- 【MySQL】MySQL索引背后的之使用策略及优化【转】
转自:http://database.ctocio.com.cn/353/11664853.shtml 另外很不错的对于索引及索引优化的文章: http://www.cnblogs.com/magia ...
- C++中的位运算总结
1)位运算 位运算是指对转换成二进制的数字进行每一位上的0.1的运算,运算涉及到五种运算:与(&),或(|),异或(^),左移(<<),右移(>>). 如下表所示: ...
- Android窗口为弹出框样式
1.XML android:theme="@android:style/Theme.Dialog <?xml version="1.0" encoding=&quo ...
- IIS报错 未将对象引用设置到对象的实例。
在vs中运行正常的项目 ,发布到IIS总是提示 未将对象引用设置到对象的实例. 运行静态页面 html正常,只是打开.aspx页面的时候报错,在确保了数据库,配置,权限均正常的情况下. 错误原因:先安 ...
- crontab的使用说明
网上瞎转载的,仅供参考 名称 : crontab 使用权限 : 所有使用者 使用方式 : crontab file [-u user]-用指定的文件替代目前的crontab. crontab-[-u ...