ObservableDictionary.cs (4334B)
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using Windows.Foundation.Collections; 5 6 namespace File360.Common 7 { 8 /// <summary> 9 /// Implementation of IObservableMap that supports reentrancy for use as a default view 10 /// model. 11 /// </summary> 12 public class ObservableDictionary : IObservableMap<string, object> 13 { 14 private class ObservableDictionaryChangedEventArgs : IMapChangedEventArgs<string> 15 { 16 public ObservableDictionaryChangedEventArgs(CollectionChange change, string key) 17 { 18 this.CollectionChange = change; 19 this.Key = key; 20 } 21 22 public CollectionChange CollectionChange { get; private set; } 23 public string Key { get; private set; } 24 } 25 26 private Dictionary<string, object> _dictionary = new Dictionary<string, object>(); 27 public event MapChangedEventHandler<string, object> MapChanged; 28 29 private void InvokeMapChanged(CollectionChange change, string key) 30 { 31 var eventHandler = MapChanged; 32 if (eventHandler != null) 33 { 34 eventHandler(this, new ObservableDictionaryChangedEventArgs(change, key)); 35 } 36 } 37 38 public void Add(string key, object value) 39 { 40 this._dictionary.Add(key, value); 41 this.InvokeMapChanged(CollectionChange.ItemInserted, key); 42 } 43 44 public void Add(KeyValuePair<string, object> item) 45 { 46 this.Add(item.Key, item.Value); 47 } 48 49 public bool Remove(string key) 50 { 51 if (this._dictionary.Remove(key)) 52 { 53 this.InvokeMapChanged(CollectionChange.ItemRemoved, key); 54 return true; 55 } 56 return false; 57 } 58 59 public bool Remove(KeyValuePair<string, object> item) 60 { 61 object currentValue; 62 if (this._dictionary.TryGetValue(item.Key, out currentValue) && 63 Object.Equals(item.Value, currentValue) && this._dictionary.Remove(item.Key)) 64 { 65 this.InvokeMapChanged(CollectionChange.ItemRemoved, item.Key); 66 return true; 67 } 68 return false; 69 } 70 71 public object this[string key] 72 { 73 get 74 { 75 return this._dictionary[key]; 76 } 77 set 78 { 79 this._dictionary[key] = value; 80 this.InvokeMapChanged(CollectionChange.ItemChanged, key); 81 } 82 } 83 84 public void Clear() 85 { 86 var priorKeys = this._dictionary.Keys.ToArray(); 87 this._dictionary.Clear(); 88 foreach (var key in priorKeys) 89 { 90 this.InvokeMapChanged(CollectionChange.ItemRemoved, key); 91 } 92 } 93 94 public ICollection<string> Keys 95 { 96 get { return this._dictionary.Keys; } 97 } 98 99 public bool ContainsKey(string key) 100 { 101 return this._dictionary.ContainsKey(key); 102 } 103 104 public bool TryGetValue(string key, out object value) 105 { 106 return this._dictionary.TryGetValue(key, out value); 107 } 108 109 public ICollection<object> Values 110 { 111 get { return this._dictionary.Values; } 112 } 113 114 public bool Contains(KeyValuePair<string, object> item) 115 { 116 return this._dictionary.Contains(item); 117 } 118 119 public int Count 120 { 121 get { return this._dictionary.Count; } 122 } 123 124 public bool IsReadOnly 125 { 126 get { return false; } 127 } 128 129 public IEnumerator<KeyValuePair<string, object>> GetEnumerator() 130 { 131 return this._dictionary.GetEnumerator(); 132 } 133 134 System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() 135 { 136 return this._dictionary.GetEnumerator(); 137 } 138 139 public void CopyTo(KeyValuePair<string, object>[] array, int arrayIndex) 140 { 141 int arraySize = array.Length; 142 foreach (var pair in this._dictionary) 143 { 144 if (arrayIndex >= arraySize) break; 145 array[arrayIndex++] = pair; 146 } 147 } 148 } 149 }