PageRenderTime 35ms CodeModel.GetById 8ms app.highlight 20ms RepoModel.GetById 1ms app.codeStats 0ms

/MVVMSidekick/MVVMSidekick.Shared/Collections.cs

https://github.com/zoujuny/MVVM-Sidekick
C# | 1039 lines | 684 code | 228 blank | 127 comment | 61 complexity | a3905258175e1472a075219e8693bf65 MD5 | raw file
   1using System;
   2using System.Collections.Generic;
   3using System.Linq;
   4using System.Text;
   5using System.ComponentModel;
   6using System.Linq.Expressions;
   7using System.Runtime.Serialization;
   8using System.Reflection;
   9using System.Threading.Tasks;
  10using System.Threading;
  11using System.Windows.Input;
  12using MVVMSidekick.ViewModels;
  13using MVVMSidekick.Commands;
  14using System.Runtime.CompilerServices;
  15using MVVMSidekick.Reactive;
  16using System.Reactive.Linq;
  17using System.Reactive.Subjects;
  18using System.Reactive;
  19using MVVMSidekick.EventRouting;
  20using System.Collections.ObjectModel;
  21using System.Collections.Specialized;
  22using System.IO;
  23using System.Collections;
  24
  25#if NETFX_CORE
  26using Windows.UI.Xaml;
  27using Windows.UI.Xaml.Data;
  28using Windows.UI.Xaml.Controls;
  29using System.Collections.Concurrent;
  30using Windows.UI.Xaml.Navigation;
  31
  32using Windows.UI.Xaml.Controls.Primitives;
  33using Windows.Foundation.Collections;
  34
  35#elif WPF
  36using System.Windows;
  37using System.Windows.Controls;
  38using System.Windows.Data;
  39using System.Collections.Concurrent;
  40using System.Windows.Navigation;
  41
  42using MVVMSidekick.Views;
  43using System.Windows.Controls.Primitives;
  44using MVVMSidekick.Utilities;
  45
  46#elif SILVERLIGHT_5||SILVERLIGHT_4
  47using System.Windows;
  48using System.Windows.Controls;
  49using System.Windows.Data;
  50using System.Windows.Navigation;
  51using System.Windows.Controls.Primitives;
  52#elif WINDOWS_PHONE_8||WINDOWS_PHONE_7
  53using System.Windows;
  54using System.Windows.Controls;
  55using Microsoft.Phone.Controls;
  56using System.Windows.Data;
  57using System.Windows.Navigation;
  58using System.Windows.Controls.Primitives;
  59#endif
  60
  61
  62namespace MVVMSidekick
  63{
  64
  65
  66
  67	namespace Collections
  68	{
  69
  70
  71
  72		public class DependencyObservableCollection<T> : DependencyObject, ICollection<T>, IList<T>, INotifyCollectionChanged, INotifyPropertyChanged
  73		{
  74
  75
  76
  77
  78			protected ObservableCollection<T> _core = new ObservableCollection<T>();
  79			public DependencyObservableCollection()
  80			{
  81
  82				var countBinding = new Binding();
  83				countBinding.Path = new PropertyPath("Count");
  84				countBinding.Mode = BindingMode.OneWay;
  85				countBinding.Source = _core;
  86				BindingOperations.SetBinding(this, CountProperty, countBinding);
  87
  88			}
  89
  90
  91
  92
  93			public void Add(T item)
  94			{
  95				_core.Add(item);
  96			}
  97
  98			public void Clear()
  99			{
 100				_core.Clear();
 101			}
 102
 103			public bool Contains(T item)
 104			{
 105				return _core.Contains(item);
 106			}
 107
 108			public void CopyTo(T[] array, int arrayIndex)
 109			{
 110				_core.CopyTo(array, arrayIndex);
 111			}
 112
 113
 114
 115
 116
 117			public virtual int Count
 118			{
 119				get { return (int)GetValue(CountProperty); }
 120				protected set { SetValue(CountProperty, value); }
 121			}
 122
 123			// Using a DependencyProperty as the backing store for Count.  This enables animation, styling, binding, etc...
 124			public static readonly DependencyProperty CountProperty =
 125				DependencyProperty.Register("Count", typeof(int), typeof(DependencyObservableCollection<T>), new PropertyMetadata(0));
 126
 127
 128
 129
 130			public bool IsReadOnly
 131			{
 132				get { return false; }
 133			}
 134
 135			public bool Remove(T item)
 136			{
 137				return _core.Remove(item);
 138			}
 139
 140			public IEnumerator<T> GetEnumerator()
 141			{
 142				return _core.GetEnumerator();
 143			}
 144
 145			IEnumerator IEnumerable.GetEnumerator()
 146			{
 147				return _core.GetEnumerator();
 148			}
 149
 150			public event NotifyCollectionChangedEventHandler CollectionChanged
 151			{
 152				add { _core.CollectionChanged += value; }
 153				remove { _core.CollectionChanged -= value; }
 154			}
 155
 156			public event PropertyChangedEventHandler PropertyChanged
 157			{
 158				add { ((INotifyPropertyChanged)_core).PropertyChanged += value; }
 159				remove { ((INotifyPropertyChanged)_core).PropertyChanged -= value; }
 160			}
 161
 162
 163			public int IndexOf(T item)
 164			{
 165				return _core.IndexOf(item);
 166			}
 167
 168			public void Insert(int index, T item)
 169			{
 170				_core.Insert(index, item);
 171			}
 172
 173			public void RemoveAt(int index)
 174			{
 175				_core.RemoveAt(index);
 176			}
 177
 178			public T this[int index]
 179			{
 180				get
 181				{
 182					return _core[index];
 183				}
 184				set
 185				{
 186					_core[index] = value;
 187				}
 188			}
 189		}
 190
 191
 192
 193#if NETFX_CORE
 194
 195		//public 
 196		/// <summary>
 197		/// ObservableVector that raises events for IObservableCollection and IObservableVector
 198		/// </summary>
 199		/// <typeparam name="T"></typeparam>
 200		public class ObservableVector<T> : ObservableCollection<T>, Windows.Foundation.Collections.IObservableVector<object>
 201		{
 202
 203
 204
 205			//// *** Constants ***
 206
 207			//private const string PropertyNameCount = "Count";
 208			//private const string PropertyNameIndexer = "Item[]";
 209
 210			//// *** Events ***
 211
 212			//public event PropertyChangedEventHandler PropertyChanged;
 213			//public event NotifyCollectionChangedEventHandler CollectionChanged;
 214
 215			// *** Constructors ***
 216
 217			public ObservableVector()
 218				: base()
 219			{
 220			}
 221
 222			public ObservableVector(IList<T> list)
 223				: base(list)
 224			{
 225			}
 226
 227			// *** Protected Methods ***   
 228
 229			protected override void ClearItems()
 230			{
 231
 232				base.ClearItems();
 233				//OnPropertyChanged(PropertyNameCount);
 234				//OnPropertyChanged(PropertyNameIndexer);
 235				//OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
 236				if (VectorChanged != null)
 237				{
 238					VectorChanged(this, new VectorChangedEventArgs { CollectionChange = Windows.Foundation.Collections.CollectionChange.Reset });
 239				}
 240			}
 241
 242			protected override void InsertItem(int index, T item)
 243			{
 244				base.InsertItem(index, item);
 245				//OnPropertyChanged(PropertyNameCount);
 246				//OnPropertyChanged(PropertyNameIndexer);
 247				//OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, item, index));
 248				if (VectorChanged != null)
 249				{
 250					VectorChanged(this, new VectorChangedEventArgs { CollectionChange = Windows.Foundation.Collections.CollectionChange.ItemInserted, Index = (uint)index });
 251				}
 252			}
 253
 254			protected override void RemoveItem(int index)
 255			{
 256				T oldItem = base[index];
 257				base.RemoveItem(index);
 258				//OnPropertyChanged(PropertyNameCount);
 259				//OnPropertyChanged(PropertyNameIndexer);
 260				//OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, oldItem, index));
 261				if (VectorChanged != null)
 262				{
 263					VectorChanged(this, new VectorChangedEventArgs { CollectionChange = Windows.Foundation.Collections.CollectionChange.ItemRemoved, Index = (uint)index });
 264				}
 265			}
 266
 267			protected override void SetItem(int index, T item)
 268			{
 269				T oldItem = base[index];
 270				base.SetItem(index, item);
 271				//OnPropertyChanged(PropertyNameIndexer);
 272				//OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Replace, item, oldItem, index));
 273				if (VectorChanged != null)
 274				{
 275					VectorChanged(this, new VectorChangedEventArgs { CollectionChange = Windows.Foundation.Collections.CollectionChange.ItemChanged, Index = (uint)index });
 276				}
 277			}
 278
 279			protected void OnPropertyChanged(string propertyName)
 280			{
 281				OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
 282			}
 283
 284			// *** Event Handlers ***
 285
 286			//protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
 287			//{
 288			//    if (PropertyChanged != null)
 289			//        PropertyChanged(this, e);
 290			//}
 291
 292			//protected virtual void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
 293			//{
 294			//    if (CollectionChanged != null)
 295			//        CollectionChanged(this, e);
 296			//}
 297
 298			public event Windows.Foundation.Collections.VectorChangedEventHandler<object> VectorChanged;
 299
 300
 301
 302			#region IList<object> Members
 303
 304			int IList<object>.IndexOf(object item)
 305			{
 306				return IndexOf((T)item);
 307			}
 308
 309			void IList<object>.Insert(int index, object item)
 310			{
 311				Insert(index, (T)item);
 312			}
 313
 314
 315
 316			object IList<object>.this[int index]
 317			{
 318				get
 319				{
 320					return this[index];
 321				}
 322				set
 323				{
 324					this[index] = (T)value;
 325				}
 326			}
 327
 328			#endregion
 329
 330			#region ICollection<object> Members
 331
 332			void ICollection<object>.Add(object item)
 333			{
 334				Add((T)item);
 335			}
 336
 337
 338			bool ICollection<object>.Contains(object item)
 339			{
 340				return Contains((T)item);
 341			}
 342
 343			void ICollection<object>.CopyTo(object[] array, int arrayIndex)
 344			{
 345
 346				for (int i = 0; i < Count; i++)
 347				{
 348					var idx = arrayIndex + i;
 349					array[idx] = this[i];
 350
 351				}
 352			}
 353
 354
 355
 356			bool ICollection<object>.Remove(object item)
 357			{
 358				return Remove((T)item);
 359			}
 360
 361			#endregion
 362
 363			#region IEnumerable<object> Members
 364
 365			IEnumerator<object> IEnumerable<object>.GetEnumerator()
 366			{
 367				return this.OfType<Object>().GetEnumerator();
 368			}
 369
 370			#endregion
 371
 372			#region IEnumerable Members
 373
 374			IEnumerator IEnumerable.GetEnumerator()
 375			{
 376				return this.GetEnumerator();
 377			}
 378
 379			#endregion
 380
 381			#region ICollection<object> Members
 382
 383
 384			public bool IsReadOnly
 385			{
 386				get { return false; }
 387			}
 388
 389			#endregion
 390		}
 391
 392
 393
 394
 395		/// <summary>
 396		/// Provides data for the changed events of a vector
 397		///// </summary>
 398		public class VectorChangedEventArgs : Windows.Foundation.Collections.IVectorChangedEventArgs
 399		{
 400			#region IVectorChangedEventArgs Members
 401			/// <summary>
 402			/// Describes the change that caused the change
 403			/// </summary>
 404			public Windows.Foundation.Collections.CollectionChange CollectionChange
 405			{
 406				get;
 407				set;
 408			}
 409			/// <summary>
 410			/// The index of the item changed
 411			/// </summary>
 412			public uint Index
 413			{
 414				get;
 415				set;
 416			}
 417			#endregion
 418		}
 419
 420#endif
 421		/// <summary>
 422		/// <para > The extension method for collections </para>
 423		/// <para>集合类型的扩展方法</para>
 424		/// </summary>
 425		public static class CollectionExtensions
 426		{
 427			/// <summary>
 428			/// <para>Transform to a dictionary with INotifyCollectionChanged</para>
 429			/// <para>生成一个带有集合变化通知的字典</para>
 430			/// </summary>
 431			/// <typeparam name="K"><para>Key Type</para><para>键类型</para></typeparam>
 432			/// <typeparam name="V"><para>Value Type</para><para>值类型</para></typeparam>
 433			/// <param name="items"><para>Source Dictionary</para><para>来源字典</para><para></para></param>
 434			/// <returns></returns>
 435			public static KeyedObserableCollection<K, V> ToKeyedObserableCollection<K, V>(this IDictionary<K, V> items)
 436			{
 437				return new KeyedObserableCollection<K, V>(items);
 438
 439			}
 440
 441
 442		}
 443
 444		public class KeyedObserableCollection<K, V> : ObservableCollection<KeyValuePair<K, V>>
 445		{
 446
 447			public KeyedObserableCollection(IDictionary<K, V> items)
 448			{
 449				if (items == null)
 450				{
 451					throw new ArgumentException("items could not be null.");
 452				}
 453				var bak = items.ToList();
 454				_coreDictionary = items;
 455				items.Clear();
 456				foreach (var item in bak)
 457				{
 458					base.Add(item);
 459				}
 460			}
 461
 462
 463
 464			IDictionary<K, V> _coreDictionary;
 465			int _coreVersion;
 466			int _shadowVersion;
 467			private void IncVer()
 468			{
 469				_coreVersion++;
 470				if (_coreVersion >= 1024 * 1024 * 1024)
 471				{
 472					_coreVersion = 0;
 473				}
 474			}
 475
 476
 477
 478			protected override void ClearItems()
 479			{
 480				base.ClearItems();
 481				_coreDictionary.Clear();
 482				IncVer();
 483			}
 484
 485
 486			protected override void InsertItem(int index, KeyValuePair<K, V> item)
 487			{
 488				_coreDictionary.Add(item.Key, item.Value);
 489				base.InsertItem(index, item);
 490				IncVer();
 491			}
 492
 493			protected override void SetItem(int index, KeyValuePair<K, V> item)
 494			{
 495
 496				_coreDictionary.Add(item.Key, item.Value);
 497				RemoveFromDic(index);
 498
 499				base.SetItem(index, item);
 500				IncVer();
 501			}
 502
 503			private void RemoveFromDic(int index)
 504			{
 505				var rem = base[index];
 506				if (rem.Key != null)
 507				{
 508					_coreDictionary.Remove(rem.Key);
 509				}
 510				IncVer();
 511			}
 512
 513			protected override void RemoveItem(int index)
 514			{
 515				RemoveFromDic(index);
 516				base.RemoveItem(index);
 517				IncVer();
 518			}
 519
 520
 521#if SILVERLIGHT_5||NET40||WINDOWS_PHONE_7
 522			Dictionary<K, V> _shadowDictionary;
 523			public IDictionary<K, V> DictionaryItems
 524			{
 525				get
 526				{
 527					if (_shadowDictionary == null || _shadowVersion != _coreVersion)
 528					{
 529						_shadowDictionary = new Dictionary<K, V>(_coreDictionary);
 530						_shadowVersion = _coreVersion;
 531					}
 532					return _shadowDictionary;
 533
 534				}
 535			}
 536
 537#else
 538			ReadOnlyDictionary<K, V> _shadowDictionary;
 539			public IDictionary<K, V> DictionaryItems
 540			{
 541				get
 542				{
 543					if (_shadowDictionary == null || _shadowVersion != _coreVersion)
 544					{
 545						_shadowDictionary = new ReadOnlyDictionary<K, V>(_coreDictionary);
 546						_shadowVersion = _coreVersion;
 547					}
 548					return _shadowDictionary;
 549
 550				}
 551			}
 552
 553
 554#endif
 555
 556
 557
 558		}
 559
 560
 561
 562#if NETFX_CORE
 563
 564		namespace CollectionView
 565		{
 566
 567
 568
 569
 570
 571			public class CollectionViewIncrementalLoader<T> : ISupportIncrementalLoading
 572			{
 573				private Func<CollectionView<T>, int, Task<IncrementalLoadResult<T>>> _loadMore;
 574				private Func<CollectionView<T>, bool> _hasMore;
 575				bool _hasNoMore = false;
 576				public CollectionViewIncrementalLoader(Func<CollectionView<T>, int, Task<IncrementalLoadResult<T>>> loadMore = null, Func<CollectionView<T>, bool> hasMore = null)
 577				{
 578					var canlm = (loadMore != null);
 579					var canhm = (hasMore != null);
 580
 581					if (canlm && canhm)
 582					{
 583
 584						_loadMore = loadMore;
 585						_hasMore = hasMore;
 586					}
 587					else
 588					{
 589						throw new InvalidOperationException("need both loadMore and hasMore have value ");
 590					}
 591				}
 592
 593				public CollectionView<T> CollectionView { get; set; }
 594
 595				#region ISupportIncrementalLoading Members
 596
 597
 598
 599				public bool HasMoreItems
 600				{
 601					get
 602					{
 603						if (!_hasNoMore)
 604						{
 605							_hasNoMore = !_hasMore(CollectionView);
 606						}
 607						return !_hasNoMore;
 608
 609					}
 610				}
 611
 612				async Task<LoadMoreItemsResult> InternalLoadMoreItemsAsync(uint count)
 613				{
 614					var rval = await _loadMore(CollectionView, (int)count);
 615
 616					_hasNoMore = !rval.HaveMore;
 617
 618					foreach (var x in rval.NewItems)
 619					{
 620						CollectionView.Add(x);
 621
 622					}
 623					return new LoadMoreItemsResult { Count = count };
 624				}
 625
 626				#endregion
 627
 628				#region ISupportIncrementalLoading Members
 629
 630
 631				public Windows.Foundation.IAsyncOperation<LoadMoreItemsResult> LoadMoreItemsAsync(uint count)
 632				{
 633					return InternalLoadMoreItemsAsync(count).AsAsyncOperation();
 634				}
 635
 636				#endregion
 637			}
 638
 639			public struct IncrementalLoadResult<T>
 640			{
 641				public IList<T> NewItems { get; set; }
 642				public bool HaveMore { get; set; }
 643			}
 644
 645			public class CollectionViewGroupCollectionItem : ICollectionViewGroup
 646			{
 647
 648				public static CollectionViewGroupCollectionItem Create(object group, IObservableVector<object> items)
 649				{
 650					return new CollectionViewGroupCollectionItem(group, items);
 651				}
 652
 653				public CollectionViewGroupCollectionItem(object group, IObservableVector<object> items)
 654				{
 655					Group = group;
 656					GroupItems = items;
 657				}
 658
 659				public object Group { get; private set; }
 660				public IObservableVector<object> GroupItems { get; private set; }
 661
 662
 663
 664			}
 665
 666			public abstract class CollectionViewGroupCollection<TItem> : ObservableVector<CollectionViewGroupCollectionItem>
 667			{
 668				public static CollectionViewGroupCollection<TItem, TGroupKey, TGroup> Create<TGroupKey, TGroup>(Func<TItem, TGroupKey> groupKeyGetter, Func<TItem, TGroup> groupFactory, Dictionary<TGroupKey, CollectionViewGroupCollectionItem> index = null)
 669				{
 670					return new CollectionViewGroupCollection<TItem, TGroupKey, TGroup>(groupKeyGetter, groupFactory, index);
 671
 672				}
 673
 674				public abstract void AddItemToGroups(object item);
 675
 676
 677				public abstract void RemoveItemFromGroups(object item);
 678			}
 679			public class CollectionViewGroupCollection<TItem, TGroupKey, TGroup> : CollectionViewGroupCollection<TItem>
 680			{
 681				public CollectionViewGroupCollection(Func<TItem, TGroupKey> groupKeyGetter, Func<TItem, TGroup> groupFactory, Dictionary<TGroupKey, CollectionViewGroupCollectionItem> index = null)
 682				{
 683					_groupKeyGetter = groupKeyGetter;
 684					_groupFactory = groupFactory;
 685
 686					_index = index ?? new Dictionary<TGroupKey, CollectionViewGroupCollectionItem>();
 687				}
 688				Func<TItem, TGroupKey> _groupKeyGetter;
 689
 690				Dictionary<TGroupKey, CollectionViewGroupCollectionItem> _index;
 691				private Func<TItem, TGroup> _groupFactory;
 692
 693
 694				public override void AddItemToGroups(object item)
 695				{
 696					var itm = (TItem)item;
 697					var key = _groupKeyGetter(itm);
 698					CollectionViewGroupCollectionItem grp;
 699					if (!_index.TryGetValue(key, out grp))
 700					{
 701						grp = CollectionViewGroupCollectionItem.Create(_groupFactory(itm), new ObservableVector<TItem>());
 702						_index.Add(key, grp);
 703						Add(grp);
 704					}
 705
 706					grp.GroupItems.Add(item);
 707
 708				}
 709
 710				public override void RemoveItemFromGroups(object item)
 711				{
 712					var key = _groupKeyGetter((TItem)item);
 713					CollectionViewGroupCollectionItem grp;
 714					if (_index.TryGetValue(key, out grp))
 715					{
 716						grp.GroupItems.Remove(item);
 717						if (grp.GroupItems.Count == 0)
 718						{
 719							_index.Remove(key);
 720							Remove(grp);
 721						}
 722					}
 723
 724				}
 725
 726			}
 727
 728			/// <summary>
 729			/// <para>ICollectionView generic implmention</para>
 730			/// <para>ICollectionView 的泛型实现</para>
 731			/// </summary>
 732			/// <typeparam name="T"><para>Content Type</para><para>内容类型</para> </typeparam>
 733			public class CollectionView<T> : ObservableVector<T>, ICollectionView
 734			{
 735				/// <summary>
 736				/// <para>Constructor of Collection View</para>
 737				/// <para>构造函数</para>
 738				/// </summary>
 739				/// <param name="items"><para>Initialing Items</para><para>初始内容集合</para></param>
 740				/// <param name="loader"><para>increanatal loader</para><para>自增加载器</para></param>
 741				public CollectionView(
 742							IEnumerable<T> items = null,
 743							CollectionViewIncrementalLoader<T> loader = null)
 744					: base(items.ToArray())
 745				{
 746
 747					items = items ?? new T[0];
 748
 749					_loader = loader;
 750					if (loader != null)
 751					{
 752						loader.CollectionView = this;
 753					}
 754
 755				}
 756
 757				/// <summary>
 758				/// <para>Constructor of Collection View</para>
 759				/// <para>构造函数</para>
 760				/// </summary>
 761				/// <param name="items"><para>Initialing Items</para><para>初始内容集合</para></param>
 762				/// <param name="groupCollection"><para>Initaling Groups</para><para>初始分组</para></param>
 763				public CollectionView(
 764					IEnumerable<T> items,
 765				CollectionViewGroupCollection<T> groupCollection)
 766					: base(items.ToArray())
 767				{
 768
 769					_group = groupCollection;
 770
 771
 772					if (_group != null && items != null)
 773					{
 774						foreach (var item in items)
 775						{
 776							_group.AddItemToGroups(item);
 777						}
 778					}
 779				}
 780				/// <summary>
 781				/// <para>Insert Item</para><para>插入</para>
 782				/// </summary>
 783				/// <param name="index"><para>targeting index</para><para>目标索引</para></param>
 784				/// <param name="item"><para> Item inserting</para><para>插入项</para></param>
 785				protected override void InsertItem(int index, T item)
 786				{
 787					base.InsertItem(index, item);
 788					if (_group != null)
 789					{
 790						_group.AddItemToGroups(item);
 791					}
 792				}
 793
 794				protected override void SetItem(int index, T item)
 795				{
 796					var oldItem = base.Items[index];
 797					if (_group != null)
 798					{
 799						_group.RemoveItemFromGroups(item);
 800					}
 801					base.SetItem(index, item);
 802					if (_group != null)
 803					{
 804						_group.AddItemToGroups(item);
 805					}
 806				}
 807				/// <summary>
 808				/// <para>Clear Items</para>
 809				/// <para>清除内容</para>
 810				/// </summary>
 811				protected override void ClearItems()
 812				{
 813					base.ClearItems();
 814					if (_group != null)
 815					{
 816						_group.Clear();
 817					}
 818				}
 819
 820				protected override void RemoveItem(int index)
 821				{
 822					var olditem = base.Items[index];
 823					base.RemoveItem(index);
 824
 825					if (_group != null)
 826					{
 827						_group.RemoveItemFromGroups(olditem);
 828					}
 829				}
 830
 831				/// <summary>
 832				/// <para>Incremental Loader</para>
 833				/// <para>自增读取器</para>
 834				/// </summary>
 835				protected CollectionViewIncrementalLoader<T> _loader;
 836
 837				Windows.Foundation.Collections.IObservableVector<object> ThisVector { get { return this; } }
 838
 839				#region ICollectionView Members
 840
 841				private CollectionViewGroupCollection<T> _group;
 842				/// <summary>
 843				/// <para>Collection Groups</para>
 844				/// <para>集合中的分组</para>
 845				/// </summary>
 846				public Windows.Foundation.Collections.IObservableVector<object> CollectionGroups
 847				{
 848					get { return _group; }
 849				}
 850
 851				/// <summary>
 852				/// <para>Fired when current Item has changed</para>
 853				/// <para>当前项变化后触发</para>
 854				/// </summary>
 855				public event EventHandler<object> CurrentChanged;
 856				/// <summary>
 857				/// <para>Fired when current Item is changing</para>
 858				/// <para>当前项变化前触发</para>
 859				/// </summary>
 860				public event CurrentChangingEventHandler CurrentChanging;
 861				/// <summary>
 862				/// <para>Current Item </para>
 863				/// <para>当前项</para 
 864				/// </summary>
 865				public object CurrentItem
 866				{
 867					get
 868					{
 869						if (Count > _CurrentPosition && _CurrentPosition >= 0)
 870						{
 871							return Items[_CurrentPosition];
 872						}
 873						return null;
 874					}
 875				}
 876
 877				int _CurrentPosition = 0;
 878				/// <summary>
 879				/// <para>Current Item Index</para><para>当前项的索引</para>
 880				/// </summary>
 881				public int CurrentPosition
 882				{
 883					get { return _CurrentPosition; }
 884					set
 885					{
 886						_CurrentPosition = value;
 887						base.OnPropertyChanged("CurrentPosition");
 888						base.OnPropertyChanged("CurrentItem");
 889					}
 890				}
 891
 892
 893				/// <summary>
 894				/// <para>Has more items can loaded by loader</para><para>是否还有更多的数据</para>
 895				/// </summary>
 896				public bool HasMoreItems
 897				{
 898					get
 899					{
 900						if (_loader == null)
 901						{
 902							return false;
 903						}
 904						else
 905						{
 906							return _loader.HasMoreItems;
 907						}
 908					}
 909				}
 910
 911
 912				/// <summary>
 913				/// <para>Is Current Item is beyond  bound of collection</para><para>当前项目是否已经超出集合最大范围</para>
 914				/// </summary>
 915				public bool IsCurrentAfterLast
 916				{
 917					get { return _CurrentPosition >= this.Count; }
 918				}
 919				/// <summary>
 920				/// <para>Is Current Item is before  bound of collection</para><para>当前项目是否已经在集合之前</para>
 921				/// </summary>
 922
 923				public bool IsCurrentBeforeFirst
 924				{
 925					get { return _CurrentPosition < 0; }
 926				}
 927
 928				/// <summary>
 929				/// <para>Load More Items</para><para>加载更多的项</para>
 930				/// </summary>
 931				/// <param name="count"><para>count of items</para><para>个数</para></param>
 932				/// <returns></returns>
 933				public Windows.Foundation.IAsyncOperation<LoadMoreItemsResult> LoadMoreItemsAsync(uint count)
 934				{
 935					if (_loader != null)
 936					{
 937						return _loader.LoadMoreItemsAsync(count);
 938					}
 939					else
 940					{
 941						throw new InvalidOperationException("this instance does not support load More Items");
 942					}
 943				}
 944
 945				bool RaiseCurrentChangingAndReturnCanceled(object newValue)
 946				{
 947					if (CurrentChanging != null)
 948					{
 949						var e = new CurrentChangingEventArgs(true);
 950
 951						CurrentChanging(this, e);
 952						return e.Cancel;
 953					}
 954					return false;
 955				}
 956				void RaiseCurrentChanged(object newValue)
 957				{
 958					if (CurrentChanged != null)
 959					{
 960						CurrentChanged(this, newValue);
 961					}
 962					base.OnPropertyChanged("CurrentItem");
 963				}
 964
 965
 966				public bool MoveCurrentToFirst()
 967				{
 968					var newIndex = 0;
 969					return MoveCurrentToPosition(newIndex);
 970
 971				}
 972
 973
 974
 975				public bool MoveCurrentToLast()
 976				{
 977					var newIndex = Count - 1;
 978					return MoveCurrentToPosition(newIndex);
 979				}
 980
 981				public bool MoveCurrentToNext()
 982				{
 983					var newIndex = CurrentPosition + 1;
 984					return MoveCurrentToPosition(newIndex);
 985				}
 986
 987				public bool MoveCurrentToPosition(int index)
 988				{
 989
 990					if (Count > 0 && index >= 0 && index < Count)
 991					{
 992
 993						var newVal = Items[index];
 994						if (RaiseCurrentChangingAndReturnCanceled(newVal))
 995						{
 996							CurrentPosition = index;
 997						}
 998						return true;
 999					}
1000					else
1001					{
1002						return false;
1003					}
1004
1005				}
1006
1007				public bool MoveCurrentToPrevious()
1008				{
1009					var newIndex = CurrentPosition - 1;
1010					return MoveCurrentToPosition(newIndex);
1011				}
1012
1013
1014
1015				public bool MoveCurrentTo(object item)
1016				{
1017					var index = IndexOf((T)item);
1018					return MoveCurrentToPosition(index);
1019				}
1020
1021				#endregion
1022
1023			}
1024
1025			//public class GroupedCollectionView<TGroup, TItem> : ObservableVector<TItem>
1026			//{
1027			//    public GroupedCollectionView(IEnumerable<TGroup> groups, Func<IEnumerable<TGroup>, IEnumerable<TItem>> itemSelector) :
1028			//        base(itemSelector(groups).ToArray())
1029			//    {
1030
1031			//    }
1032			//}
1033		}
1034
1035#endif
1036
1037	}
1038
1039}