Last active
January 2, 2016 06:18
-
-
Save JohanLarsson/8262180 to your computer and use it in GitHub Desktop.
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
public static class OcExt | |
{ | |
public static void InvokeAdd<T>(this ObservableCollection<T> collection, T newItem) | |
{ | |
collection.Invoke(()=>collection.Add(newItem)); | |
} | |
public static void InvokeAddRange<T>(this ObservableCollection<T> collection, IEnumerable<T> newItems) | |
{ | |
collection.Invoke(() => | |
{ | |
foreach (var newItem in newItems) | |
{ | |
collection.Add(newItem); | |
} | |
}); | |
} | |
public static void InvokeRemove<T>(this ObservableCollection<T> collection, T oldItem) | |
{ | |
collection.Invoke(() => collection.Remove(oldItem)); | |
} | |
public static void InvokeClear<T>(this ObservableCollection<T> collection) | |
{ | |
collection.Invoke(() => collection.Clear()); | |
} | |
public static void Invoke<T>(this ObservableCollection<T> col, Action action) | |
{ | |
Dispatcher dispatcher = Application.Current != null | |
? Application.Current.Dispatcher | |
: Dispatcher.CurrentDispatcher; | |
dispatcher.Invoke(action); | |
} | |
public static DispatcherOperation AddAsync<T>(this ObservableCollection<T> collection, T newItem) | |
{ | |
return collection.InvokeAsync(() => collection.Add(newItem)); | |
} | |
public static DispatcherOperation AddRangeAsync<T>(this ObservableCollection<T> collection, IEnumerable<T> newItems) | |
{ | |
return collection.InvokeAsync(() => | |
{ | |
foreach (var newItem in newItems) | |
{ | |
collection.Add(newItem); | |
} | |
}); | |
} | |
public static DispatcherOperation RemoveAsync<T>(this ObservableCollection<T> collection, T oldItem) | |
{ | |
return collection.InvokeAsync(() => collection.Remove(oldItem)); | |
} | |
public static DispatcherOperation ClearAsync<T>(this ObservableCollection<T> collection) | |
{ | |
return collection.InvokeAsync(() => collection.Clear()); | |
} | |
public static DispatcherOperation InvokeAsync<T>(this ObservableCollection<T> col, Action action) | |
{ | |
Dispatcher dispatcher = Application.Current != null | |
? Application.Current.Dispatcher | |
: Dispatcher.CurrentDispatcher; | |
return dispatcher.InvokeAsync(action); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment