【问题标题】:Listview fails to display text of list items when item count exceeds 400 items当项目数超过 400 项时,Listview 无法显示列表项的文本
【发布时间】:2014-08-25 02:26:22
【问题描述】:

我观察到当项目数超过 400 项时,我的应用程序中的 Listview 无法显示 listview 项目的文本。

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

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

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

这是我的 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 中显示大量项目。

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

这行得通:

<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>

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

【问题讨论】:

    标签: wpf winrt-xaml windows-8.1 windows-phone-8.1


    【解决方案1】:

    是的,您不能将它们全部放在内存中。 您可以使用虚拟化。 这是链接:http://msdn.microsoft.com/en-us/library/system.windows.controls.virtualizingstackpanel.isvirtualizing.aspx

    【讨论】:

    • 我研究了虚拟化文档。我已经设置了一个高度和宽度,添加了一个循环,并添加了一个 ItemsStackPanel。不幸的是,它只是不能解决我的问题。我已经用代码 sn-p 更新了我的帖子。
    【解决方案2】:

    请记住,这是一部电话,您为什么希望一开始就列出 400 多个项目?想一想用户将如何处理这些数据。
    如果您有超过 400 个项目,也许您应该有一些选择来缩小范围。也许你可以有结果“页面”,并将它们集中在一个易于消化的数字中,既适用于手机,更重要的是适用于用户。

    话虽如此,除了 Rang 的回答之外,您也许可以尝试为您的应用程序分配更多内存。看看this post,但具体来说:

    <FunctionalCapabilities>
      <FunctionalCapability Name="ID_FUNCCAP_EXTEND_MEM"/>
    </FunctionalCapabilities>
    

    【讨论】:

    • Windows Phone 8.1 似乎不支持此功能。
    • 很奇怪。这是另一个资源,表明上述内容应该可以工作,但提供了另一种选择:second article。可以肯定的是,您不是在处理低内存设备,对吧?
    • 这适用于 Windows Phone 8.1。因此,与较大的设备相比,所有内存都在一定程度上受到限制。我在我的诺基亚 1520 上观察到这一点,这是一款高性能的移动设备。
    • Windows Phone 8.1 不支持此功能。不过还是谢谢。
    【解决方案3】:

    我的列表显示了几个具有空字符串的项目。

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

    结果我这样做了:

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

    【讨论】:

      猜你喜欢
      • 2015-06-21
      • 2017-11-06
      • 2016-06-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多