【问题标题】:When I Add many Items into ListBox, I get OutOfMemoryException . How to modify it?当我将许多项目添加到 ListBox 中时,我得到 OutOfMemoryException 。如何修改它?
【发布时间】:2012-02-22 23:13:33
【问题描述】:

这是我的列表框:

XMLA:

<Style x:Key="ListBoxStyle" TargetType="ListBox">
        <Setter Property="Foreground" Value="{StaticResource PhoneForegroundBrush}"/>
        <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Disabled"/>
        <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ListBox">
                    <ScrollViewer x:Name="ScrollViewer">
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="{TemplateBinding Height}"/>
                                <RowDefinition Height="100"/>
                            </Grid.RowDefinitions>
                            <ItemsPresenter Grid.Row="0"/>
                            <Button Content="Add" Grid.Row="1" Click="Button_Click"/>
                        </Grid>
                    </ScrollViewer>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>  <ListBox Style="{StaticResource ListBoxStyle}" Name="listBox" Height="600" ItemsSource="{Binding MyData}">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel>
                            <TextBlock Text="{Binding Name}"/>
                            <Image Source="{Binding Img}" Stretch="UniformToFill"/>
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>

代码隐藏:

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        for (int i = 0; i < 50; i++)
        {
            MyData.Add(new Data { Name = i.ToString(), Img = "/Background.png" });
        }
    }

当我单击更多按钮时,我得到了 OutOfMemoryException。

但是,如果我不设置 ListBox 样式。我将项目添加到列表框中,项目是工作。

【问题讨论】:

    标签: windows-phone-7


    【解决方案1】:

    当您重新模板 ListBox 时,您将失去数据虚拟化。因此,您所有的项目图像一直都在内存中。你可以减小图像的大小以避免高内存消耗吗?

    【讨论】:

    • 哦。我的 ListBox 必须有图像。你能告诉我如何重新模板化我的 ListBox 并且不会丢失数据虚拟化吗?
    • 其中一种方法可以做到这一点:删除“更多”Button 并检测底部压缩状态以加载下一个项目
    • 我找到了一个解决方案用户Scrollviewer和itemsControl组成一个ListBox
    • @Ku6opr 我以为虚拟化是通过VirtualizingStackPanel实现的,和retemplate没有关系??
    【解决方案2】:

    我想,要启用虚拟化,您应该更改 ListBox ControlTemplate。将除 ItemsPresenter 之外的所有内容移出 ScrollViewer:

    <ControlTemplate TargetType="ListBox">
      <Grid>
         <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition Height="100"/>
         </Grid.RowDefinitions>
         <ScrollViewer x:Name="ScrollViewer" Grid.Row="0">
              <ItemsPresenter />
         </ScrollViewer>
         <Button Content="Add" Grid.Row="1" Click="Button_Click"/>
      </Grid>
    </ControlTemplate>
    

    并确保您的 MyData 实现 IList 接口。

    【讨论】:

    • MyData 实现 IList 接口。
    • 将除 ItemsPresenter 之外的所有内容移出 ScrollViewer 。虚拟化仍处于禁用状态
    • 在这种情况下,也许值得为 ListBox 使用默认的 ControlTemplate 并在 ListBox 附近放置“添加”按钮。感谢您发布此问题,了解此类限制非常重要。我以前从未读过。
    • 如果编辑 ListBox 的 Style ,ListBox 虚拟化禁用eugenedotnet.com/2011/05/…
    猜你喜欢
    • 1970-01-01
    • 2013-07-07
    • 1970-01-01
    • 2013-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    相关资源
    最近更新 更多