[BUG] The application crashes after adding a collection to the ItemsSource
ORRNY66 opened this issue · 2 comments
ORRNY66 commented
Platform:
- OS: Android
- Device: AndroidEmulator and physical device
- Sdk vervion: Android SDK 29/30
- Sharpnado.CollectionView: 2.0.0
- Xamarin.Forms: 5.0.0.2012 or 5.0.0.2337
- Xamarin.Essentials: 1.6.1 or 1.7.1
Describe the bug
I have the following code:
Code behind
`<renderedViews:CollectionView
x:Name="collectionView"
ItemHeight="50"
ItemSpacing="5">
<renderedViews:CollectionView.ItemTemplate>
<DataTemplate>
<Grid HeightRequest="50">
</Grid>
</DataTemplate>
</renderedViews:CollectionView.ItemTemplate>
</renderedViews:CollectionView>
private ObservableCollection<string> MyCollection { get; set; }
public MainPage()
{
InitializeComponent();
MyCollection = new ObservableCollection<string>();
for (int i = 0; i < 10; i++)
{
MyCollection.Add(i.ToString());
}
collectionView.ItemsSource = MyCollection;
}`
or MVVM
`<renderedViews:CollectionView
x:Name="collectionView"
ItemsSource="{Binding MyCollection}"
ItemHeight="50"
ItemSpacing="5">
<renderedViews:CollectionView.ItemTemplate>
<DataTemplate>
<Grid HeightRequest="50">
</Grid>
</DataTemplate>
</renderedViews:CollectionView.ItemTemplate>
</renderedViews:CollectionView>`
public MainViewModel()
{
MyCollection = new ObservableCollection<string>();
for (int i = 0; i < 10; i++)
{
MyCollection.Add(i.ToString());
}
}
private ObservableCollection<string> _myCollection;
public ObservableCollection<string> MyCollection
{
get => _myCollection;
set => SetAndRaise(ref _myCollection, value);
}`
I tried different ways via MVVM, code behind. The same exception every time.
Exceptions
Unhandled Exception:
Exception:System.InvalidCastException: Specified cast is not valid.
at Sharpnado.CollectionView.Droid.Renderers.CollectionViewRenderer+RecycleViewAdapter.CreateView (Xamarin.Forms.ViewCell& viewCell, System.Int32 itemViewType) [0x0001f] in D:\Dev\Sharpnado\src\Sharpnado.HorizontalListView\Sharpnado.CollectionView.Droid\Renderers\CollectionViewRenderer.RecycleViewAdapter.cs:393
at Sharpnado.CollectionView.Droid.Renderers.CollectionViewRenderer+RecycleViewAdapter.CreateViewHolder (System.Int32 itemViewType) [0x0000d] in D:\Dev\Sharpnado\src\Sharpnado.HorizontalListView\Sharpnado.CollectionView.Droid\Renderers\CollectionViewRenderer.RecycleViewAdapter.cs:326
at Sharpnado.CollectionView.Droid.Renderers.CollectionViewRenderer+RecycleViewAdapter.<.ctor>b__17_0 () [0x00000] in D:\Dev\Sharpnado\src\Sharpnado.HorizontalListView\Sharpnado.CollectionView.Droid\Renderers\CollectionViewRenderer.RecycleViewAdapter.cs:145
at Sharpnado.CollectionView.Droid.Renderers.ViewHolderQueue.<BuildViewsAsync>b__7_0 () [0x00004] in D:\Dev\Sharpnado\src\Sharpnado.HorizontalListView\Sharpnado.CollectionView.Droid\Renderers\ViewHolderQueue.cs:73
at System.Threading.Tasks.Task.InnerInvoke () [0x0000f] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/external/corert/src/System.Private.CoreLib/src/System/Threading/Tasks/Task.cs:2476
at System.Threading.Tasks.Task.Execute () [0x00000] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/external/corert/src/System.Private.CoreLib/src/System/Threading/Tasks/Task.cs:2319
--- End of stack trace from previous location where exception was thrown ---
roubachof commented
your DataTemplate view should be wrapped in a ViewCell
Deleted user commented
Thanks a lot. My ignorance :)