PagedCollectionView taskListView = new PagedCollectionView(cache.Collection4UI); public class TaskCache : SyncCache<Task> { protected override void DoUpdateCache(ObservableCollection<Task> collection) { Random random = new Random(102200); int toremove = random.Next(collection.Count); if (toremove > 0) { collection.RemoveAt(toremove); } for (int i = 1; i <= 5; i++) { collection.Add(new Task() { Area = i / 3D, ProjectName = "公司 " + ((i % 5) + 1).ToString(), TaskName = "Task " + i.ToString(), //Project = list1[i % list1.Count], DueDate = DateTime.Now.AddDays(i), Complete = (i % 2 == 0), Notes = "Task " + i.ToString() + " is due on " + DateTime.Now.AddDays(i) + ". Lorum ipsum..." }); } } } public abstract class SyncCache<T> where T : new() { private ObservableCollection<T> _Collection; private ObservableCollection4UI<T> _Collection4UI; private Timer _Timer; public SyncCache() { _Collection = new ObservableCollection<T>(); _Collection4UI = new ObservableCollection4UI<T>(_Collection, Deployment.Current.Dispatcher); _Timer = new Timer(CacheUpdateTimerCall); } public ObservableCollection4UI<T> Collection4UI { get { return _Collection4UI; } } public void StartTimer() { _Timer.Change(1000, 1000); } public void StopTimer() { _Timer.Change(-1, -1); } protected abstract void DoUpdateCache(ObservableCollection<T> collection); private void CacheUpdateTimerCall(object state) { _Timer.Change(-1, -1); try { this.DoUpdateCache(_Collection); } finally { _Timer.Change(1000, 1000); } } } public class ObservableCollection4UI<T> : ICollection<T>, INotifyCollectionChanged { public ObservableCollection4UI(ObservableCollection<T> collection, Dispatcher dispatcher) { this._Source = collection; this._Source.CollectionChanged += new NotifyCollectionChangedEventHandler(Source_CollectionChanged); this._Dispatcher = dispatcher; } void Source_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e) { this.OnCollectionChanged(e); } private ObservableCollection<T> _Source; private Dispatcher _Dispatcher; protected void OnCollectionChanged(NotifyCollectionChangedEventArgs e) { this._Dispatcher.BeginInvoke(() => { if (CollectionChanged != null) { this.CollectionChanged(this, e); } } ); } public bool Contains(T item) { return _Source.Contains(item); } public void CopyTo(T[] array, int arrayIndex) { _Source.CopyTo(array, arrayIndex); } public int Count { get { return _Source.Count; } } public bool IsReadOnly { get { return true; } } #region ICollection<T> 成员 void ICollection<T>.Add(T item) { throw new NotImplementedException(); } void ICollection<T>.Clear() { throw new NotImplementedException(); } bool ICollection<T>.Remove(T item) { throw new NotImplementedException(); } #endregion #region IEnumerable<T> 成员 IEnumerator<T> IEnumerable<T>.GetEnumerator() { return _Source.GetEnumerator(); } #endregion #region IEnumerable 成员 IEnumerator IEnumerable.GetEnumerator() { return _Source.GetEnumerator(); } #endregion #region INotifyCollectionChanged 成员 public event NotifyCollectionChangedEventHandler CollectionChanged; #endregion }