Migrando seu App do Windows Phone para o Windows 8 RC – DependencyObjectCollection
Escrito June 29th, 2012 . Nenhum comentário .
Se você utiliza a classe DependencyObjectCollection para criar listas de DependencyObjects em seus controles no Windows Phone, vai sentir falta dela no Windows 8.
Eu a utilizo no Wp7Tools para fazer uma attached property que mapeia eventos da View para actions no ViewModel, desacoplando os dois.
Caso você também precise, segue abaixo a implementação:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
public class DependencyObjectCollection<T> : DependencyObject, IList<T>, INotifyCollectionChanged where T : DependencyObject { private ObservableCollection<T> Items = new ObservableCollection<T>(); public DependencyObjectCollection() { Items.CollectionChanged += Items_CollectionChanged; } private void Items_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e) { OnCollectionChanged(e); } public void Add(T item) { Items.Add(item); } public void Clear() { Items.Clear(); } public bool Contains(T item) { return Contains(item); } public void CopyTo(T[] array, int arrayIndex) { Items.CopyTo(array, arrayIndex); } public int Count { get { return Items.Count; } } public bool IsReadOnly { get { return false; } } public bool Remove(T item) { return Items.Remove(item); } public IEnumerator<T> GetEnumerator() { return Items.GetEnumerator(); } IEnumerator IEnumerable.GetEnumerator() { return Items.GetEnumerator(); } public int IndexOf(T item) { return Items.IndexOf(item); } public void Insert(int index, T item) { Items.Insert(index, item); } public void RemoveAt(int index) { Items.RemoveAt(index); } public T this[int index] { get { return Items[index]; } set { Items[index] = value; } } public event NotifyCollectionChangedEventHandler CollectionChanged; private void OnCollectionChanged(NotifyCollectionChangedEventArgs args) { NotifyCollectionChangedEventHandler handler = CollectionChanged; if (handler != null) { handler(this, args); } } } |
Bom proveito :)