我从xml中提取了1000个条目,并将它们加载到一个列表对象中。List被数据绑定到水平方向的ListBox,因此用户可以从左到右或从右到左翻阅项目。由于项目数量很大,我的应用程序退出了,可能是因为内存使用过多。如果我将条目减少到50个,它就起作用了。
我找到了这篇文章http://shawnoster.com/blog/post/Improving-ListBox-Performance-in-Silverlight-for-Windows-Phone-7-Data-Virtualization.aspx
然后这篇关于数据虚拟化的文章
http://blogs.msdn.com/b/ptorr/archive/2010/08/16/virtualizing-data-in-windows-phone-7-silverlight-applications.aspx
在实现了一个实现了IList的虚拟化类之后,我看不出有什么不同。这个below仍然被调用了1000次,尽管我预计它只会被调用30-40次,因为我知道UI已经在列表框中虚拟化了。为什么虚拟化没有发挥作用?
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,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>发布于 2011-09-19 02:35:07
这是导致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,50,10,0" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.Background>
<SolidColorBrush />
</ListBox.Background>
</ListBox>发布于 2015-06-25 11:16:58
在接下来的文章中,解释了在ScrollViewer中包装虚拟化UI控件将为其提供无限的空间,从而有效地禁用UI虚拟化
http://blogs.msdn.com/b/mcsuksoldev/archive/2010/04/13/performance-characteristics-of-the-silverlight-datagrid.aspx
https://stackoverflow.com/questions/7441510
复制相似问题