当项目数超过 400 项时,Listview 无法显示列表项的文本 [英] Listview fails to display text of list items when item count exceeds 400 items

查看:22
本文介绍了当项目数超过 400 项时,Listview 无法显示列表项的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现当项目数超过 400 项时,我的应用中的 Listview 无法显示列表视图项的文本.

I am observing that the Listview in my app fails to display the text of listview items when the item count exceeds 400 items.

我猜测这是手机的内存问题,而不是较大设备的模式.

My guess is that this is a memory issue with the phone and not a pattern with larger devices.

我假设我只需要添加我的列表视图的 itemsource 所基于的项目的一小部分.

I assume that I will need to only add a fraction of the items that my listview's itemsource is based on.

管理负责显示大量数据的列表视图的最佳方法是什么?

What is the best approach for managing a listview that's responsible for displaying large amounts of data?

这是我的 XAML:

<ListView x:Name="ContactList" ScrollViewer.VerticalScrollBarVisibility="Visible"
         attachedProperties:CategoryHelper.Category="{Binding SelectedCategory, Mode=TwoWay}"
         ItemsSource="{Binding SelectedCategory.Contacts}"
         VirtualizingStackPanel.VirtualizationMode="Recycling" 
         SelectedItem="{Binding SelectedContact, Mode=TwoWay}"
         HorizontalContentAlignment="Left"
         Margin="58,175,0,0" 
         Height="425"
         Width="425"
         Background="Transparent" Foreground="#FF333747" VerticalAlignment="Top" Canvas.ZIndex="99" HorizontalAlignment="Left" >

    <ListView.Template>
        <ControlTemplate>
            <ScrollViewer>
                <ItemsPresenter VirtualizingStackPanel.VirtualizationMode="{TemplateBinding VirtualizingStackPanel.VirtualizationMode}" />
            </ScrollViewer>
        </ControlTemplate>
    </ListView.Template>

    <ListView.ItemsPanel>
        <ItemsPanelTemplate>
            <ItemsStackPanel/>
        </ItemsPanelTemplate>
    </ListView.ItemsPanel>

    <ListView.ItemContainerStyle>
        <Style TargetType="ListViewItem">
            <Setter Property="FontSize" Value="26" />
            <Setter Property="Margin" Value="0,10" />
            <Setter Property="Foreground" Value="#FF333747" />
        </Style>
    </ListView.ItemContainerStyle>

    <ListView.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <CheckBox Style="{StaticResource CheckBoxStyle1}"
                          Loaded="CheckBox_Loaded"
                          Visibility="{Binding ElementName=grid, Path=DataContext.BroadcastActivated, Converter={StaticResource BoolToVisibilityConverter}, Mode=TwoWay}"
                          Margin="0,-8" BorderBrush="#FF4E58BC" Checked="ContactChecked" Unchecked="ContactUnchecked">
                </CheckBox>

                <TextBlock Text="{Binding DisplayName}">
                    <Interactivity:Interaction.Behaviors>
                        <Core:EventTriggerBehavior EventName="Holding">
                            <behaviors:MoveContactAction />
                        </Core:EventTriggerBehavior>
                    </Interactivity:Interaction.Behaviors>

                    <FlyoutBase.AttachedFlyout>
                        <MenuFlyout>
                            <MenuFlyoutItem Text="Family" Command="{Binding ElementName=grid, Path=DataContext.MoveCommand}" CommandParameter="{Binding RelativeSource={RelativeSource Self}, Path=Text}" />
                            <MenuFlyoutItem Text="Friend" Command="{Binding ElementName=grid, Path=DataContext.MoveCommand}" CommandParameter="{Binding RelativeSource={RelativeSource Self}, Path=Text}" />
                            <MenuFlyoutItem Text="Business" Command="{Binding ElementName=grid, Path=DataContext.MoveCommand}" CommandParameter="{Binding RelativeSource={RelativeSource Self}, Path=Text}" />
                            <MenuFlyoutItem Text="Others" Command="{Binding ElementName=grid, Path=DataContext.MoveCommand}" CommandParameter="{Binding RelativeSource={RelativeSource Self}, Path=Text}" />
                        </MenuFlyout>
                    </FlyoutBase.AttachedFlyout>
                </TextBlock>
            </StackPanel>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

更新:我现在可以在 Listview 中显示大量项目.

Update: I can now get a large amount of items to display in a Listview.

但是,如果没有观察到项目未显示的相同问题,我将无法应用 DataTemplate.

However, I am unable to apply a DataTemplate without observing the same issue of items not being displayed.

这有效:

<ListView x:Name="ContactList" ItemsSource="{Binding SelectedCategory.Contacts}"
            Height="425"
            Width="425"
            Margin="58,175,0,0"  Canvas.ZIndex="99"
            Background="Transparent" Foreground="#FF333747" 
            VerticalAlignment="Top" HorizontalAlignment="Left">

    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <VirtualizingStackPanel />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>

    <ItemsControl.Template>
        <ControlTemplate TargetType="ItemsControl">
            <Border>
                <ScrollViewer>
                    <ItemsPresenter/>
                </ScrollViewer>
            </Border>
        </ControlTemplate>
    </ItemsControl.Template>

    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="Hello Wworld" />
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ListView>

这不会:

<ListView x:Name="ContactList" ItemsSource="{Binding SelectedCategory.Contacts}"
            Height="425"
            Width="425"
            Margin="58,175,0,0"  Canvas.ZIndex="99"
            Background="Transparent" Foreground="#FF333747" 
            VerticalAlignment="Top" HorizontalAlignment="Left">

    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <VirtualizingStackPanel />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>

    <ItemsControl.Template>
        <ControlTemplate TargetType="ItemsControl">
            <Border>
                <ScrollViewer>
                    <ItemsPresenter/>
                </ScrollViewer>
            </Border>
        </ControlTemplate>
    </ItemsControl.Template>

    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding DisplayName}" />
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ListView>

似乎一旦应用绑定,使用数据模板显示列表视图项就会失败.我该如何解决?

推荐答案

我的列表显示了几个包含空字符串的项目.

My list was displaying several items that had empty strings.

由于此列表按升序排序,显示空文本的项目显示在列表的开头,而其他项目确实存在但不在滚动查看器的视口内.

As a result of this list being sorted in ascending order, the items that displayed empty text showed up at the beginning of the list while the other items did exist but were not within the scrollviewer's viewport.

结果我这样做了:

if (string.IsNullOrWhiteSpace(contact.DisplayName))
{
    continue;
}

这篇关于当项目数超过 400 项时,Listview 无法显示列表项的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆