Skip to content

Instantly share code, notes, and snippets.

@runceel
Created April 21, 2012 13:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save runceel/2437074 to your computer and use it in GitHub Desktop.
Save runceel/2437074 to your computer and use it in GitHub Desktop.
WinRT IObservableVector<T> implementation
namespace Okazuki.WinRT.Common
{
using System.Collections.Generic;
using System.Linq;
using Windows.Foundation.Collections;
class ObservableVector<T> : IObservableVector<T>
{
private List<T> inner;
public ObservableVector() : this(Enumerable.Empty<T>())
{
}
public ObservableVector(IEnumerable<T> source)
{
this.inner = new List<T>(source);
}
public event VectorChangedEventHandler<T> VectorChanged;
private void RaiseVectorChanged(CollectionChange collectionChange, int index)
{
var h = this.VectorChanged;
if (h != null)
{
h(this, new VectorChangedEventArgs { CollectionChange = collectionChange, Index = (uint)index });
}
}
public int IndexOf(T item)
{
return this.inner.IndexOf(item);
}
public void Insert(int index, T item)
{
this.inner.Insert(index, item);
this.RaiseVectorChanged(CollectionChange.ItemInserted, index);
}
public void RemoveAt(int index)
{
this.inner.RemoveAt(index);
this.RaiseVectorChanged(CollectionChange.ItemRemoved, index);
}
public T this[int index]
{
get
{
return this.inner[index];
}
set
{
this.inner[index] = value;
this.RaiseVectorChanged(CollectionChange.ItemChanged, index);
}
}
public void Add(T item)
{
this.inner.Add(item);
this.RaiseVectorChanged(CollectionChange.ItemInserted, this.inner.Count - 1);
}
public void Clear()
{
this.inner.Clear();
this.RaiseVectorChanged(CollectionChange.Reset, 0);
}
public bool Contains(T item)
{
return this.inner.Contains(item);
}
public void CopyTo(T[] array, int arrayIndex)
{
this.inner.CopyTo(array, arrayIndex);
}
public int Count
{
get { return this.inner.Count; }
}
public bool IsReadOnly
{
get { return false; }
}
public bool Remove(T item)
{
var index = this.inner.IndexOf(item);
if (index == -1)
{
return false;
}
this.RemoveAt(index);
return true;
}
public IEnumerator<T> GetEnumerator()
{
return this.inner.GetEnumerator();
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return this.inner.GetEnumerator();
}
class VectorChangedEventArgs : IVectorChangedEventArgs
{
public CollectionChange CollectionChange { get; set; }
public uint Index { get; set; }
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment