certainly, the error was in casting an object to a type not compatible with the object itself.
this is the instruction that fixed my problem
IList collection = (IList)collectionView.ItemsSource;
I put the complete code below
protected override DataTemplate OnSelectTemplate(object item, BindableObject container)
{
var collectionView = (CollectionView)container;
IList collection = (IList)collectionView.ItemsSource;
var index = collection.IndexOf(item);
return index%2==0 ? unoTemplate : dueTemplate;
}