我有1000個來自xml的項目並將它們加載到List對象中。列表是數據綁定到水平方向的ListBox,因此用戶可以翻閱項目從左到右或從右到左。由於項目數量巨大,我的應用可能由於內存使用過多而退出。如果我把這些物品減少到50個,它就會奏效。ListBox數據虛擬化沒有生效
,然後這篇文章中的數據虛擬化
實現虛擬化類實現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,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>
你應該張貼您的列表框的XAML - 從你的描述我懷疑你是OVERR將ItemsPanelTemplate以水平方向放置到StackPanel,因此該列表實際上不再是虛擬化的...... –
的確,我們需要XAML,因爲數據虛擬化和UI虛擬化是兩個不同的主題,而且這個問題顯然是關於UI虛擬化的。 –
剛剛粘貼了XAML,但由於某種原因,頂標籤被切斷。基本上這部分 –
climbon