Skip to content

Instantly share code, notes, and snippets.

@JohanLarsson
Last active January 2, 2016 06:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JohanLarsson/8262180 to your computer and use it in GitHub Desktop.
Save JohanLarsson/8262180 to your computer and use it in GitHub Desktop.
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