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 ...
随机推荐
- win7下eclipse中文字显示过小
用win7英文版系统,在eclipse里输入中文字的时候小的可怜,今天实在忍不下去了,随网上搜了搜找到了解决办法,记录下来备用. 操作步骤:打开Elcipse --点击菜单栏上的 “Windows”— ...
- 洛谷P2725 邮票 Stamps
P2725 邮票 Stamps 37通过 224提交 题目提供者该用户不存在 标签USACO 难度普及/提高- 提交 讨论 题解 最新讨论 为什么RE?在codevs上AC的. 题目背景 给一组 ...
- linux Apache和php配置
今天安装Apache httpd web服务器,安装过程分为三部分: (1)./configure (2)make (3)make install (需要root权限) 我的apache 安装在/ho ...
- java中HashMap的用法
重点介绍HashMap.首先介绍一下什么是Map.在数组中我们是通过数组下标来对其内容索引的,而在Map中我们通过对象来对对象进行索引,用来索引的对象叫做key,其对应的对象叫做value.在下文中会 ...
- sublime Text2 2.0.2 build 2221 64位 破解(已测试)
近终于找到 sublime Text2 升级到 2.0.2 build 2221 64位 的破破解 输入注册码就成了 ----- BEGIN LICENSE ----- Andrew Weber S ...
- leetcode 107
107. Binary Tree Level Order Traversal II Given a binary tree, return the bottom-up level order trav ...
- Javascrpt
HTML HTML概述: HTML是英文Hyper Text Mark-up Language(超文本标记语言)的缩写,他是一种制作万维网页面标准语言(标记).相当于定义统一的一套规则,大家都来遵守他 ...
- javaSE第十四天
第十四天 92 1. 正则表达式(理解) 92 (1)定义: 92 (2)常见规则 92 A:字符 92 B:字符类 93 C:预定义字符类 93 D:边界匹 ...
- Java设计模式(Design Patterns In Java)读书摘要——第1章 绪论
为何需要模式 模式是做事的方法,是实现目标,研磨技术的方法.通俗点说,模式是为了解决某个行业的某个问题的有效的方法或技艺. 为何需要设计模式 为了提升代码的水准,是代码变得简洁而易用.模式是一种思想, ...
- wpf仿QQ之窗体翻转
很久以前在网上找过窗体翻转的Demo,但看得不是很明白,大多是内容的翻转,研究了下,现在分享给大家. 利用UIElement.RenderTransform 属性就能实现元素的呈现位置的转换,因此只需 ...