歴史的な目的のために、人々を「現在の」経路に置く...それは私ですMicrosoftがVisual Studio 2012のWindows Storeの「基本ページ」テンプレートでこの要件を解決したことを知るためには重要です.LayeredAwarePageをサポートするために、これらは非公開のObservableDictionaryクラスを生成します。
しかし、IDictionaryではなく新しいIObservableMapインターフェイスを直接実装しています。このインターフェイスは、Windows.Foundation.Collections名前空間で定義されたMapChangedイベントとMapChangedEventHandlerを追加します。
以下のスニペットは、プロジェクトの「共通」フォルダに生成LayoutAwarePage.csからわずかObservableDictionaryクラスです:新しいWindows.Foundation.Collections名前空間の
/// <summary>
/// Implementation of IObservableMap that supports reentrancy for use as a default view
/// model.
/// </summary>
private class ObservableDictionary<K, V> : IObservableMap<K, V>
{
private class ObservableDictionaryChangedEventArgs : IMapChangedEventArgs<K>
{
public ObservableDictionaryChangedEventArgs(CollectionChange change, K key)
{
CollectionChange = change;
Key = key;
}
public CollectionChange CollectionChange { get; private set; }
public K Key { get; private set; }
}
private Dictionary<K, V> _dictionary = new Dictionary<K, V>();
public event MapChangedEventHandler<K, V> MapChanged;
private void InvokeMapChanged(CollectionChange change, K key)
{
var eventHandler = MapChanged;
if (eventHandler != null)
{
eventHandler(this, new ObservableDictionaryChangedEventArgs(change, key));
}
}
public void Add(K key, V value)
{
_dictionary.Add(key, value);
InvokeMapChanged(CollectionChange.ItemInserted, key);
}
public void Add(KeyValuePair<K, V> item)
{
Add(item.Key, item.Value);
}
public bool Remove(K key)
{
if (_dictionary.Remove(key))
{
InvokeMapChanged(CollectionChange.ItemRemoved, key);
return true;
}
return false;
}
public bool Remove(KeyValuePair<K, V> item)
{
V currentValue;
if (_dictionary.TryGetValue(item.Key, out currentValue) &&
Object.Equals(item.Value, currentValue) && _dictionary.Remove(item.Key))
{
InvokeMapChanged(CollectionChange.ItemRemoved, item.Key);
return true;
}
return false;
}
public V this[K key]
{
get
{
return _dictionary[key];
}
set
{
_dictionary[key] = value;
InvokeMapChanged(CollectionChange.ItemChanged, key);
}
}
public void Clear()
{
var priorKeys = _dictionary.Keys.ToArray();
_dictionary.Clear();
foreach (var key in priorKeys)
{
InvokeMapChanged(CollectionChange.ItemRemoved, key);
}
}
public ICollection<K> Keys
{
get { return _dictionary.Keys; }
}
public bool ContainsKey(K key)
{
return _dictionary.ContainsKey(key);
}
public bool TryGetValue(K key, out V value)
{
return _dictionary.TryGetValue(key, out value);
}
public ICollection<V> Values
{
get { return _dictionary.Values; }
}
public bool Contains(KeyValuePair<K, V> item)
{
return _dictionary.Contains(item);
}
public int Count
{
get { return _dictionary.Count; }
}
public bool IsReadOnly
{
get { return false; }
}
public IEnumerator<KeyValuePair<K, V>> GetEnumerator()
{
return _dictionary.GetEnumerator();
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return _dictionary.GetEnumerator();
}
public void CopyTo(KeyValuePair<K, V>[] array, int arrayIndex)
{
if (array == null) throw new ArgumentNullException("array");
int arraySize = array.Length;
foreach (var pair in _dictionary)
{
if (arrayIndex >= arraySize) break;
array[arrayIndex++] = pair;
}
}
}
さらなる検査はの負荷を示しています新しいインターフェイスが定義されていますが、1つだけPropertySetクラスが実装されています。実際、これはかなり良いObservableDictionary自体のようです。しかし、MSが依然として私立ObservableDictionaryを生成する理由がなければならない。したがって、ここでは賛否両論を特定するためにさらに検討が必要です。
要するに、PropertySetまたはObservableDictionaryに基づく独自のIObservableMapは、現在のWindows 8および8プロジェクトの即時要件を解決する必要があります。しかし、古いフレームワーク(WPF 4とPhone 7.5)では、さらに多くの作業が必要です。
あなたがやろうとしているものに関して、もう少し詳細を与えることができますか?あなたは "一般的な辞書を渡す"と言うときに書くことができるようにしたいコードの例を表示できますか? – JMarsch
私は異なる辞書を持っています。郵便番号と市。 私は何をしようとしています: - データ(モデル/辞書)をWPF ItemsControlにバインドします。郵便番号の都市を変更すると、モデルが自動的に更新されます。残念ながら、私はINotifyPropertyChangedが必要なので、 "普通の"ディクショナリでOneWay-Bindingしか使用できません。 - INotifyPropertyChangedを実装し、辞書を含むObservableDictionaryを作成します。 –
解決策はあります:http://stackoverflow.com/questions/5663395/net-observabledictionary –