ListView项未从SQLite数据库[Xamarin.Forms]完全加载

时间:2019-10-29 04:20:26

标签: c# android sqlite xamarin.forms xamarin.forms.listview

我最近注意到我的列表视图和使用sqlite和Xamarin.Forms的本地数据库存在问题,我的问题是当表的数据转储到我的ObservableCollection(ListView的源项)中时,我的列表视图显示它,ListView不会显示应该显示的所有项目,我尝试通过将(没有数据库的)项目动态添加到我的List View中来解决,问题似乎消失了,我确信问题不是来自我的sqlite数据库,还因为一个过去的项目中的问题,我使用一个api获取我的信息,问题是相同的,所以我得出的结论是,该问题正在将外部信息显示到ListView中,所以有人知道换句话说,如果有人可以尝试使用listview和sqlite数据库进行项目,那么这可能是解决我的问题的好方法。

重要事实: XAML中的ListView具有Frame控制器,因此当我不使用它时,它将向我显示数据库中的所有项目,我也不知道为什么

我当前的XAML代码:

      <ListView
           SeparatorVisibility="None"
                IsGroupingEnabled="True"
                ItemsSource="{Binding TasksCollection}"
                GroupDisplayBinding="{Binding Key}"
                HasUnevenRows="True">
                <ListView.GroupHeaderTemplate>
                    <DataTemplate>
                        <TextCell
                            Text="{Binding Key}"
                            TextColor="White"/>
                    </DataTemplate>
                </ListView.GroupHeaderTemplate>
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <Frame
                                HeightRequest="150"
                                Margin="10"
                                HasShadow="True"
                                CornerRadius="25"
                                BackgroundColor="White">
                                <Grid
                                    Padding="5">
                                <Grid.GestureRecognizers>
                                    <TapGestureRecognizer Command="{Binding GridExpandCommand}"/>
                                </Grid.GestureRecognizers>
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="2*"/>
                                    <RowDefinition Height="*"/>
                                </Grid.RowDefinitions>
                                    <Label
                                        Grid.Column="0"
                                        Grid.Row="0"
                                        Text="{Binding Name}"
                                        FontAttributes="Bold"
                                        FontSize="Small"
                                        HorizontalOptions="Start"
                                        VerticalOptions="Start">
                                    </Label>
                                    <Image
                                        HeightRequest="25"
                                        Grid.Row="0"
                                        Grid.Column="1"
                                        Source="{Binding TaskIcon}"
                                        HorizontalOptions="End"
                                        VerticalOptions="Start">
                                    </Image>
                                    <Label
                                        Grid.Row="1"
                                        Grid.Column="0"
                                        Text="{Binding Description}"
                                        FontAttributes="Bold"
                                        FontSize="Small"
                                        HorizontalOptions="Start"
                                        VerticalOptions="End">
                                    </Label>
                                    <Button
                                        Grid.Row="1"
                                        Grid.Column="1"
                                        Text="{Binding IsDone}"
                                        TextColor="White"
                                        FontAttributes="Bold"
                                        VerticalOptions="CenterAndExpand"
                                        HorizontalOptions="Center"
                                        FontSize="Small"
                                        CornerRadius="100"
                                        BackgroundColor="LawnGreen">
                                    </Button>
                                </Grid>
                            </Frame>
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>

我将项目添加到集合中的代码(我使用MvvmHelpers通过分组对数据进行分组)如下:

foreach (var item in new string[]
            { Languages.HightPriorText, Languages.MediumPriorText, Languages.LessPriorityText })
            {
                var sorted = from tasks in tableList
                             orderby tasks.PriorityLevel // orberby is a keyword of System.Linq namespace
                             group tasks by tasks.PriorityLevel into taskList
                             select new Grouping<string, TaskItemViewModel>(taskList.Key, taskList);
                List<Grouping<string, TaskItemViewModel>> listItems = sorted.Where(t => t.Key == item).ToList();
                foreach (var item2 in listItems)
                    VeryImportantList.Add(item2);
            }

结果如下:

The image of the result of the app

任何答案都会引起我的注意,对于任何问题,请提供一些帮助!

1 个答案:

答案 0 :(得分:2)

我终于解决了它,并且正如我所期望的那样,它是我当前版本的Xamarin.Forms(4.0.0)的一个错误,因此我将其更新为最新版本,并且一切恢复正常,如我所愿!