roubachof / Sharpnado.CollectionView

A performant list view supporting: grid, horizontal and vertical layout, drag and drop, and reveal animations.
The Unlicense
245 stars 30 forks source link

[BUG] The application crashes after adding a collection to the ItemsSource #57

Closed ghost closed 2 years ago

ghost commented 2 years ago

Platform:

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 2 years ago

your DataTemplate view should be wrapped in a ViewCell

ghost commented 2 years ago

Thanks a lot. My ignorance :)