【问题标题】:CollectionView Throws InvalidCastException or empty with ObservableCollectionCollectionView 抛出 InvalidCastException 或 ObservableCollection 为空
【发布时间】:2020-11-30 01:54:11
【问题描述】:

当我使用 observablecollection 加载我的代码时,我得到一个

System.InvalidCastException: '指定的强制转换无效。'

在我对代码进行一些更改之前,它只是向我显示了一个空列表(这同样令人费解,因为我唯一更改的是将 observable 集合从被封装移动到像这里一样显示)

我尝试使用列表而不是 ObservableCollection,结果为空。

将 CollectionView 更改为 ListView 可以让一切正常运行。

我在后台实现了 fody 和 autofac,实现来自一本书。

xaml:

<CollectionView x:Name="CalanderDays"
                            ItemsSource="{Binding daysInCurrentCalanderView}"
                            Grid.Row="1"
                            Grid.ColumnSpan="7">
                <CollectionView.ItemTemplate>
                <DataTemplate>
                    <ViewCell Height="48">
                        <Grid Grid.Column="1">
                             <BoxView Color="Green"
                                      CornerRadius="124"/>
                             <Label Text="{Binding WorkDay.Day}"
                                    FontSize="18"
                                    TextColor="White"
                                    FontAttributes="Bold"
                                    HorizontalOptions="Center"
                                    VerticalOptions="Center"/>
                        </Grid>
                    </ViewCell>
                </DataTemplate>
            </CollectionView.ItemTemplate>
        </CollectionView>

视图模型:

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using DatePickerEmbedded.Models;
using DatePickerEmbedded.Repositories;
using Xamarin.Forms;

namespace DatePickerEmbedded.ViewModels
{
    public class MainViewCalanderViewModel : ViewModel
    {

        private readonly WorkDaysRepository db;


        private IList<WorkDayViewModel> loadingDays;

        public ObservableCollection<WorkDayViewModel> daysInCurrentCalanderView
        {
            get;
            private set;
        }

        private DateTime lastDateChosen = DateTime.Today;
        public MainViewCalanderViewModel(WorkDaysRepository db)
        {
            this.db = db;
            Task.Run(async () => await LoadDays());
        }


        private async Task LoadDays()
        {
            loadingDays = new List<WorkDayViewModel>();
            DateTime firstDayOfMonth = new DateTime(lastDateChosen.Year, lastDateChosen.Month, 1);
            int offset = (int)firstDayOfMonth.DayOfWeek;

            int numberOfDaysShown = (int)(Math.Ceiling((DateTime.DaysInMonth(lastDateChosen.Year, lastDateChosen.Month) + (double)offset) / 7) * 7);
            
            for (int i = -offset; i <= numberOfDaysShown - offset; i++)
            {
                DateTime workingDay = firstDayOfMonth.AddDays(i);
                WorkDay dayToFill = await db.GetDay(workingDay);


                if (dayToFill == null || workingDay.Month != firstDayOfMonth.Month)
                {
                    dayToFill = new WorkDay() { ClockIn = workingDay };
                    loadingDays.Add(new WorkDayViewModel(dayToFill));
                }
                else
                {
                    loadingDays.Add(new WorkDayViewModel(dayToFill));
                }
                
            }

            daysInCurrentCalanderView = new ObservableCollection<WorkDayViewModel>(loadingDays);        
        }
    }
    

}

【问题讨论】:

  • 请在 collectionView 中没有单元格。

标签: c# xamarin.forms collectionview


【解决方案1】:

问题出在xaml,不能在CollectionView.DataTemplate中添加&lt;ViewCell&gt;,我建议你可以在&lt;DataTemplate&gt;中使用&lt;StackLayout&gt;&lt;Grid&gt;

<CollectionView x:Name="CalanderDays"
                        ItemsSource="{Binding daysInCurrentCalanderView}"
                        Grid.Row="1"
                        Grid.ColumnSpan="7">
            <CollectionView.ItemTemplate>
                <DataTemplate>
                   
                        <Grid Grid.Column="1">
                            <BoxView Color="Green"
                                  CornerRadius="124"/>
                            <Label Text="{Binding WorkDay.Day}"
                                FontSize="18"
                                TextColor="White"
                                FontAttributes="Bold"
                                HorizontalOptions="Center"
                                VerticalOptions="Center"/>
                        </Grid>
                   
                </DataTemplate>
            </CollectionView.ItemTemplate>
        </CollectionView>

【讨论】:

    猜你喜欢
    • 2020-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-02
    • 2021-10-19
    • 2023-03-29
    相关资源
    最近更新 更多