【问题标题】:app crash while loading large amount of items to listbox将大量项目加载到列表框时应用程序崩溃
【发布时间】:2013-07-25 09:44:33
【问题描述】:

我正在开发一个 windows phone8 应用程序 我有一个显示大量项目的列表框,我在列表框的数据模板中也有一个图像控件。

当我加载前 100 个项目时,它运行良好,当我加载接下来的 100 个项目(总共 200 个)时,会发生应用崩溃。

谁能帮我解决这个问题。

这是我的列表框代码

<ListBox ScrollViewer.VerticalScrollBarVisibility="Disabled" Visibility="Visible" x:Name="commentsListBox" VerticalAlignment="Top" >
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <Border BorderBrush="#FFB9B9B9" BorderThickness="0,0,0,2" Width="462" Margin="14,0,0,0">
                                <Grid VerticalAlignment="Top" Width="470" >
                                    <Image HorizontalAlignment="Left" Height="100" Width="100" VerticalAlignment="Top" Margin="10,20,0,0" Stretch="Fill" Source="{Binding profileImage}"></Image>
                                    <TextBlock Name="userNameTextBlock"  VerticalAlignment="Top" FontWeight="Bold" HorizontalAlignment="Left" Text="{Binding userName}" Foreground="Black" Height="36" Width="238" Margin="136,18,0,0" TextTrimming="WordEllipsis" FontFamily="Tahoma" />
                                    <StackPanel VerticalAlignment="Top" HorizontalAlignment="Left" Margin="146,68,0,0" Width="314">
                                        <TextBlock Name="commentTextBlock" VerticalAlignment="Top" TextAlignment="Left" HorizontalAlignment="Left" Foreground="Black" Text="{Binding comment}" Width="313" TextWrapping="Wrap" FontFamily="Tahoma" Margin="0,0,-17,0" ></TextBlock>
                                        <TextBlock Text="{Binding date}" Height="36" Foreground="Red" Width="264"  Name="commentDate" HorizontalAlignment="Right" VerticalAlignment="Bottom" Margin="0,0,10,0" TextTrimming="WordEllipsis" FontFamily="Tahoma"></TextBlock>
                                        <Button HorizontalAlignment="Right" Width="100" Content="View" Height="70" FontSize="18" x:Name="viewBtn" Background="#FFD71D26" VerticalAlignment="Bottom" Margin="0,0,-10,-10"></Button>
                                    </StackPanel>

                                </Grid>
                            </Border>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                    <!--<ListBoxItem>

            </ListBoxItem>-->
                </ListBox>

有数据的类是

 public class WishListCommentCommonFormate
{
    public string comment { get; set; }
    public string userName { get; set; }
    public DateTime date { get; set; }
    public string profileImage { get; set; }
}

这里的图片是来自网站的 URL。 我看到了Improving ListBox Performance in Silverlight for Windows Phone 7: Data Virtualization 和其他几个类似的例子,但我没有解决这个问题的想法。

请给我一些想法。 谢谢。

【问题讨论】:

  • 您的内存不足。您需要使用更少的 RAM。

标签: c# image xaml windows-phone-8 windows-phone


【解决方案1】:

两个建议。第一个如果您必须使用 ListBox,您将通过阅读ListBox why art thou blanking 获得更好的图像性能和虚拟化。在这篇来自 Windows Phone 开发团队的文章中,他们提到了另一种设置图像的方法。

<Image> 
    <Image.Source> 
        <BitmapImage UriSource="{Binding ImgUrl}" CreateOptions="BackgroundCreation"/> 
    </Image.Source> 
</Image>

第二个建议(可能更好)是不要使用 LstBox,而是使用LongListSelector。这个控件是高性能的,并且使用更少的内存。只需在 xaml 中从 ListBox 到 LongListSelector 进行查找/替换。

【讨论】:

  • 您好,感谢您的回复,我将尝试 LongListSelector 并更新响应。
猜你喜欢
  • 1970-01-01
  • 2015-02-01
  • 2020-11-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-30
  • 1970-01-01
  • 2020-03-15
相关资源
最近更新 更多