forked from Windows-XAML/Template10
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
jerrynixon
committed
Nov 2, 2016
1 parent
7a1f7ef
commit 145ae0b
Showing
92 changed files
with
2,534 additions
and
711 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Template10.BCL; | ||
|
||
namespace Template10.BCL | ||
{ | ||
public interface IService | ||
{ | ||
} | ||
|
||
public interface IInfrastructure<T> where T: IService | ||
{ | ||
T Instance { get; set; } | ||
} | ||
} | ||
|
||
namespace Template10 | ||
{ | ||
public static class Extensions | ||
{ | ||
public static T GetInfrastructure<T>(this IInfrastructure<T> accessor) where T : IService | ||
{ | ||
return accessor.Instance; | ||
} | ||
|
||
public static void SetInfrastructure<T>(this IInfrastructure<T> accessor, T value) where T : IService | ||
{ | ||
accessor.Instance = value; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.Collections.ObjectModel; | ||
using System.Collections.Specialized; | ||
using System.ComponentModel; | ||
|
||
namespace Template10.BCL | ||
{ | ||
public partial class ObservableItemCollectionEx<T> | ||
: ObservableCollection<T>, IDisposable where T : INotifyPropertyChanged | ||
{ | ||
private bool _allowRaiseCollectionChanged = true; | ||
|
||
public override event NotifyCollectionChangedEventHandler CollectionChanged; | ||
|
||
public event EventHandler<ItemPropertyChangedEventArgs> ItemPropertyChanged; | ||
|
||
public ObservableItemCollectionEx() | ||
{ | ||
base.CollectionChanged += (s, e) => | ||
{ | ||
if (_allowRaiseCollectionChanged) | ||
CollectionChanged?.Invoke(this, e); | ||
}; | ||
} | ||
|
||
public ObservableItemCollectionEx(IEnumerable<T> collection) : base(collection) | ||
{ | ||
base.CollectionChanged += (s, e) => | ||
{ | ||
if (_allowRaiseCollectionChanged) | ||
{ | ||
CollectionChanged?.Invoke(this, e); | ||
} | ||
}; | ||
} | ||
|
||
protected sealed override void OnCollectionChanged(NotifyCollectionChangedEventArgs e) | ||
{ | ||
CheckDisposed(); | ||
switch (e.Action) | ||
{ | ||
case NotifyCollectionChangedAction.Add: | ||
RegisterPropertyChanged(e.NewItems); | ||
break; | ||
case NotifyCollectionChangedAction.Move: | ||
break; | ||
case NotifyCollectionChangedAction.Remove: | ||
case NotifyCollectionChangedAction.Replace: | ||
UnRegisterPropertyChanged(e.OldItems); | ||
if (e.NewItems != null) | ||
{ | ||
RegisterPropertyChanged(e.NewItems); | ||
} | ||
|
||
break; | ||
case NotifyCollectionChangedAction.Reset: | ||
break; | ||
} | ||
base.OnCollectionChanged(e); | ||
} | ||
|
||
protected override void ClearItems() | ||
{ | ||
UnRegisterPropertyChanged(this); | ||
base.ClearItems(); | ||
} | ||
|
||
private void RegisterPropertyChanged(IList items) | ||
{ | ||
CheckDisposed(); | ||
foreach (INotifyPropertyChanged item in items) | ||
{ | ||
if (item != null) | ||
{ | ||
item.PropertyChanged += new PropertyChangedEventHandler(item_PropertyChanged); | ||
} | ||
} | ||
} | ||
|
||
private void UnRegisterPropertyChanged(IList items) | ||
{ | ||
CheckDisposed(); | ||
foreach (INotifyPropertyChanged item in items) | ||
{ | ||
if (item != null) | ||
{ | ||
item.PropertyChanged -= new PropertyChangedEventHandler(item_PropertyChanged); | ||
} | ||
} | ||
} | ||
|
||
private void item_PropertyChanged(object sender, PropertyChangedEventArgs e) | ||
{ | ||
CheckDisposed(); | ||
ItemPropertyChanged?.Invoke(this, new ItemPropertyChangedEventArgs(sender, IndexOf((T)sender), e)); | ||
} | ||
|
||
|
||
public void AddRange(IEnumerable<T> items) | ||
{ | ||
CheckDisposed(); | ||
_allowRaiseCollectionChanged = false; | ||
foreach (var item in items) | ||
{ | ||
Add(item); | ||
} | ||
_allowRaiseCollectionChanged = true; | ||
CollectionChanged?.Invoke(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, items)); | ||
} | ||
|
||
public void RemoveRange(IEnumerable<T> items) | ||
{ | ||
CheckDisposed(); | ||
_allowRaiseCollectionChanged = false; | ||
foreach (var item in items) | ||
{ | ||
Remove(item); | ||
} | ||
_allowRaiseCollectionChanged = true; | ||
CollectionChanged?.Invoke(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, items)); | ||
} | ||
|
||
bool disposed = false; | ||
public void Dispose() | ||
{ | ||
if (!disposed) | ||
{ | ||
ClearItems(); | ||
disposed = true; | ||
} | ||
} | ||
|
||
public void CheckDisposed() | ||
{ | ||
if (disposed) | ||
{ | ||
throw new ObjectDisposedException(GetType().FullName); | ||
} | ||
} | ||
|
||
public class ItemPropertyChangedEventArgs | ||
{ | ||
public ItemPropertyChangedEventArgs(object item, int changedIndex, PropertyChangedEventArgs e) | ||
{ | ||
ChangedItem = item; | ||
ChangedItemIndex = changedIndex; | ||
PropertyChangedArgs = e; | ||
} | ||
|
||
public object ChangedItem { get; set; } | ||
|
||
public int ChangedItemIndex { get; set; } | ||
|
||
public PropertyChangedEventArgs PropertyChangedArgs { get; set; } | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Windows.Foundation.Collections; | ||
|
||
namespace Template10.BCL | ||
{ | ||
public 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 RaiseMapChanged(CollectionChange change, K key) | ||
{ | ||
MapChanged?.Invoke(this, new ObservableDictionaryChangedEventArgs(change, key)); | ||
} | ||
|
||
public void Add(K key, V value) | ||
{ | ||
_dictionary.Add(key, value); | ||
RaiseMapChanged(CollectionChange.ItemInserted, key); | ||
} | ||
|
||
public void Add(KeyValuePair<K, V> item) => Add(item.Key, item.Value); | ||
|
||
public bool Remove(K key) | ||
{ | ||
if (_dictionary.Remove(key)) | ||
{ | ||
RaiseMapChanged(CollectionChange.ItemRemoved, key); | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
public bool Remove(KeyValuePair<K, V> item) | ||
{ | ||
V currentValue; | ||
if (_dictionary.TryGetValue(item.Key, out currentValue) && | ||
Equals(item.Value, currentValue) && _dictionary.Remove(item.Key)) | ||
{ | ||
RaiseMapChanged(CollectionChange.ItemRemoved, item.Key); | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
public V this[K key] | ||
{ | ||
get { return _dictionary[key]; } | ||
set | ||
{ | ||
_dictionary[key] = value; | ||
RaiseMapChanged(CollectionChange.ItemChanged, key); | ||
} | ||
} | ||
|
||
public void Clear() | ||
{ | ||
var priorKeys = _dictionary.Keys.ToArray(); | ||
_dictionary.Clear(); | ||
foreach (var key in priorKeys) | ||
{ | ||
RaiseMapChanged(CollectionChange.ItemRemoved, key); | ||
} | ||
} | ||
|
||
public ICollection<K> Keys => _dictionary.Keys; | ||
|
||
public bool ContainsKey(K key) => _dictionary.ContainsKey(key); | ||
|
||
public bool TryGetValue(K key, out V value) => _dictionary.TryGetValue(key, out value); | ||
|
||
public ICollection<V> Values => _dictionary.Values; | ||
|
||
public bool Contains(KeyValuePair<K, V> item) => _dictionary.Contains(item); | ||
|
||
public int Count => _dictionary.Count; | ||
|
||
public bool IsReadOnly => false; | ||
|
||
public IEnumerator<KeyValuePair<K, V>> GetEnumerator() => _dictionary.GetEnumerator(); | ||
|
||
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() => _dictionary.GetEnumerator(); | ||
|
||
public void CopyTo(KeyValuePair<K, V>[] array, int arrayIndex) | ||
{ | ||
int arraySize = array.Length; | ||
foreach (var pair in _dictionary) | ||
{ | ||
if (arrayIndex >= arraySize) break; | ||
array[arrayIndex++] = pair; | ||
} | ||
} | ||
|
||
public bool TrySet(K key, V value) | ||
{ | ||
try | ||
{ | ||
if (ContainsKey(key)) | ||
{ | ||
Remove(key); | ||
} | ||
Add(key, value); | ||
return true; | ||
} | ||
catch | ||
{ | ||
return false; | ||
} | ||
} | ||
|
||
public bool TryGet(K key, out V value) | ||
{ | ||
value = default(V); | ||
if (!ContainsKey(key)) | ||
{ | ||
return false; | ||
} | ||
try | ||
{ | ||
value = this[key]; | ||
return true; | ||
} | ||
catch | ||
{ | ||
return false; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.