Windows phone 7 ListBox数据虚拟化未生效
我从xml中获取了1000个条目,并将它们加载到一个列表对象中。列表被数据绑定到水平方向的列表框,所以用户可以从左向右或从右向左翻转项目。由于项目数量巨大,我的应用程序可能由于内存使用过多而退出。如果我把项目减少到50个,它就成功了 我找到了这篇文章 然后是关于数据虚拟化的文章 在实现了一个实现IList的虚拟化类之后,我看不出有什么不同。this[](如下)仍被调用了1000次,尽管我预计只会被调用30-40次,因为我知道UI已经在Listbox中虚拟化了。为什么虚拟化不起作用Windows phone 7 ListBox数据虚拟化未生效,windows-phone-7,listbox,virtualization,Windows Phone 7,Listbox,Virtualization,我从xml中获取了1000个条目,并将它们加载到一个列表对象中。列表被数据绑定到水平方向的列表框,所以用户可以从左向右或从右向左翻转项目。由于项目数量巨大,我的应用程序可能由于内存使用过多而退出。如果我把项目减少到50个,它就成功了 我找到了这篇文章 然后是关于数据虚拟化的文章 在实现了一个实现IList的虚拟化类之后,我看不出有什么不同。this[](如下)仍被调用了1000次,尽管我预计只会被调用30-40次,因为我知道UI已经在Listbox中虚拟化了。为什么虚拟化不起作用 objec
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>
以下是导致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>
在下面的文章中,我们解释了在ScrollViewer中包装虚拟化UI控件将给它无限的空间,从而有效地禁用UI虚拟化
您应该发布列表框的XAML-根据您的描述,我怀疑您正在水平方向将ItemsPanelTemplate覆盖到StackPanel,因此列表确实不再是虚拟化的……事实上,我们需要XAML,因为数据虚拟化和UI虚拟化是两个不同的主题,这个问题显然是关于UI虚拟化的。只是粘贴了XAML,但由于某种原因,顶部标签被切断了。基本上,这部分Scrollviewer就是问题所在?我也遇到了同样的问题,删除了包装Scrollviewer(并添加了Scrollviewer.HorizontalScrollBarVisibility=“Auto”)为我修复了它。@DušanKnežević注意VirtualizangStackPanelScrollViewer.HorizontalScrollBarVisibility是一个附加属性,当添加到ListBox控件时,它将显示自己的滚动条。当添加到ScrollViewer中时,它会获得无限的空间,从而有效地关闭UI虚拟化