かずきのBlog@hatena

すきな言語は C# + XAML の組み合わせ。Azure Functions も好き。最近は Go 言語勉強中。日本マイクロソフトで働いていますが、ここに書いていることは個人的なメモなので会社の公式見解ではありません。

IObservableVectorの実装クラスって?

もしかして無い?こんなの毎回実装するなんて、信じられないよ。本番までには出てきて!

namespace Application3.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; }
        }

    }
}