1

I am using Xamarin Forms and their templates come with MvvMHelpers object to be used in the ViewModel as ObservableRangeCollections. I know ObservableCollections. If you try to do :

ObservableRangeCollection<Object> collection = new ObservableRangeCollection<Object>();
List<Object> objects = new List<Objects>();
collection.ReplaceRange(objects);
//error invalid type 

Does anyone know how to use an ObservableRangeCollection? There is nothing on it in Google, Bing or StackOverflow.

Try the search you'll see Xamarin is promoting something so new that nobody knows what it is.

jzeferino
  • 7,700
  • 1
  • 39
  • 59
Mark Lane
  • 214
  • 3
  • 9

4 Answers4

2

ObservableRangeCollection is a helper class by the Xamarin Evangelist James Montemagno.

The source is available in his github: https://github.com/jamesmontemagno/mvvm-helpers

ObservableRangeCollection intends to help when adding/replacing Collections to a ObservableCollection.

In a "regular" ObservableCollection, for each new item added to the Collection, a OnCollectionChanged event would raise.

This is where ObservableRangeCollection gets in. It allows to replace/add elements to the Collection without firing an event for each element.

Rodrigo Elias
  • 783
  • 7
  • 18
  • Thanks Rodrigo! this explains a lot for me. I see there is an answer from Sushi Hangover which also would be the right answer for me. But your answer was first and adds some explanation that helps me understand which to choose. ObservableCollection versus ObservableRangeCollection and the events. – Mark Lane Jul 02 '16 at 15:42
2

ObservableRangeCollection is subclassed from ObservableCollection.

So in your example, substitute your <T>, i.e:

ObservableRangeCollection<string> collection = new ObservableRangeCollection<string>(); 
List<string> objects = new List<string>(); 
collection.ReplaceRange(objects); 

Consult the code here: https://github.com/jamesmontemagno/mvvm-helpers/blob/master/MvvmHelpers/ObservableRangeCollection.cs

SushiHangover
  • 73,120
  • 10
  • 106
  • 165
  • Thanks for the response! I was doing this but ReplaceRange threw an error each time. I know that its a Read Only class but when I switched to ObservableCollection the code worked. – Mark Lane Jul 02 '16 at 15:43
1

This is not something that new. There's plenty of code using ObservableCollection.

What you are trying to achieve can be done like this:

List<Object> myList = new List<Objects>();

ObservableCollection<Object> myCollection = new ObservableCollection<Object>(myList);

Read more about ObservableCollection.

jzeferino
  • 7,700
  • 1
  • 39
  • 59
  • Hi jzeferino, I use ObservableCollection for WPF but never used ObservableRangeCollection. I didnt know it was an extension to the ObservableCollection. thanks anyhow! – Mark Lane Jul 02 '16 at 15:44
0

Check out my answer here, which is an enhanced version of ObservableRangeCollection optimized for less event raising and reuse of items in UI.

Shimmy Weitzhandler
  • 101,809
  • 122
  • 424
  • 632