I am trying to migrate a ListView to a CollectionView.
In my ListView I use a "DataTemplateSelector" with 2 pages like this:
<?xml version="1.0" encoding="UTF-8" ?>
<ViewCell
x:Class="FMPatients.Customs.IncomingViewCell"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:ffimageloading="clr-namespace:FFImageLoading.Forms;assembly=FFImageLoading.Forms">
<Grid
...
</Grid>
</ViewCell>
Since the ViewCell does not exist in the CollectionView, I decided to change the root of the Xaml for a DataTemplate.
<?xml version="1.0" encoding="UTF-8" ?>
<DataTemplate
x:Class="FMPatients.Customs.IncomingViewCell"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:ffimageloading="clr-namespace:FFImageLoading.Forms;assembly=FFImageLoading.Forms">
<Grid
...
</Grid>
</DataTemplate>
My DataTemplateSelector:
class MyDataTemplateSelector : DataTemplateSelector
{
public MyDataTemplateSelector()
{
// Retain instances!
this.incomingDataTemplate = new DataTemplate(typeof(IncomingViewCell));
this.outgoingDataTemplate = new DataTemplate(typeof(OutgoingViewCell));
}
protected override DataTemplate OnSelectTemplate(object item, BindableObject container)
{
try
{
return ((ChatMessagesVM)item).IsIncoming ? incomingDataTemplate : outgoingDataTemplate;
}
catch (System.Exception ex)
{
MicrosoftLogEvent.TrackError(ex);
return null;
}
//var messageVm = item as ChatMessagesVM;
//if (messageVm == null)
// return null;
//return messageVm.IsIncoming ? incomingDataTemplate : outgoingDataTemplate;
}
private readonly DataTemplate incomingDataTemplate;
private readonly DataTemplate outgoingDataTemplate;
}
I'm not sure if it is correct since I still get the following exception:
**System.InvalidCastException:** 'Specified cast is not valid.'
Any idea how to do the migration or what may be causing the error.
Thank you.
