我从xml获得了1000个项目并将它们加载到List对象中. List是数据绑定到ListBox,它是水平方向的,因此用户可以从左到右或从右到左翻阅项目.由于项目数量巨大,我的应用程序可能因内存使用过多而退出.如果我将项目减少到50就可以了.
我找到了这篇文章
http://shawnoster.com/blog/post/Improving-ListBox-Performance-in-Silverlight-for-Windows-Phone-7-Data-Virtualization.aspx
然后这篇关于数据虚拟化的文章
在实现实现IList的虚拟化类之后,我发现没有区别.这个[](下面)被称为1000次,虽然我预计它只会被调用30-40次,因为我知道UI已经在ListBox中虚拟化了.为什么虚拟化没有开始?
object IList.this[int index] { get { if (index >= cachedItems.Count) { //replenish cache code here } return cachedItems[index]; } set { throw new NotImplementedException(); } }
这是与问题相关的XAML部分.希望这能够全面了解代码.不确定Width = Auto是否与它有关但我无法更改它,否则我的刷卡停止.
<ScrollViewer HorizontalScrollBarVisibility="Auto" Margin="0,0" Width="auto" x:Name="WordsScrollview" Opacity="1" Grid.Row="1" RenderTransformOrigin="0.5,0.5"> <ListBox x:Name="horizontalListBox" Width="auto" Height="Auto" > <ListBox.ItemsPanel> <ItemsPanelTemplate> <StackPanel Orientation="Horizontal"> </StackPanel> </ItemsPanelTemplate> </ListBox.ItemsPanel> <ListBox.ItemTemplate> <DataTemplate> <StackPanel> <TextBlock Width="430" Text="{Binding Word}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}" TextAlignment="Center" /> <Image Height="290" HorizontalAlignment="Center" Name="image1" Stretch="Fill" Width="430" Source="{Binding ImageFile}" Margin="10,50,10,0" /> </StackPanel> </DataTemplate> </ListBox.ItemTemplate> <ListBox.Background> <SolidColorBrush /> </ListBox.Background> </ListBox> </ScrollViewer>
解决方法
以下是导致UI虚拟化最终启动的XAML.
<ListBox x:Name="horizontalListBox" Height="Auto" ScrollViewer.HorizontalScrollBarVisibility="Auto" > <ListBox.ItemsPanel> <ItemsPanelTemplate> <VirtualizingStackPanel Orientation="Horizontal"> </VirtualizingStackPanel> </ItemsPanelTemplate> </ListBox.ItemsPanel> <ListBox.ItemTemplate> <DataTemplate> <StackPanel> <TextBlock Width="430" Text="{Binding Word}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}" TextAlignment="Center" /> <Image Height="290" HorizontalAlignment="Center" Name="image1" Stretch="Fill" Width="430" Source="{Binding ImageFile}" Margin="10,0" /> </StackPanel> </DataTemplate> </ListBox.ItemTemplate> <ListBox.Background> <SolidColorBrush /> </ListBox.Background> </ListBox>