Mark Lane
Mark Lane

Reputation: 214

How to convert List<Objects> to an ObservableRangeCollection

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.

Upvotes: 1

Views: 4060

Answers (4)

Shimmy Weitzhandler
Shimmy Weitzhandler

Reputation: 104741

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

Upvotes: 0

SushiHangover
SushiHangover

Reputation: 74144

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

Upvotes: 2

Rodrigo Elias
Rodrigo Elias

Reputation: 783

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.

Upvotes: 2

jzeferino
jzeferino

Reputation: 7850

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.

Upvotes: 1

Related Questions