이 내 목록 상자입니다 :ListBox에 많은 항목을 추가하면 OutOfMemoryException이 발생합니다. 어떻게 수정합니까?
XMLA :
<Style x:Key="ListBoxStyle" TargetType="ListBox">
<Setter Property="Foreground" Value="{StaticResource PhoneForegroundBrush}"/>
<Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Disabled"/>
<Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBox">
<ScrollViewer x:Name="ScrollViewer">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="{TemplateBinding Height}"/>
<RowDefinition Height="100"/>
</Grid.RowDefinitions>
<ItemsPresenter Grid.Row="0"/>
<Button Content="Add" Grid.Row="1" Click="Button_Click"/>
</Grid>
</ScrollViewer>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style> <ListBox Style="{StaticResource ListBoxStyle}" Name="listBox" Height="600" ItemsSource="{Binding MyData}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Name}"/>
<Image Source="{Binding Img}" Stretch="UniformToFill"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
코드 숨김 : 나는 버튼이 더 누르면
private void Button_Click(object sender, RoutedEventArgs e)
{
for (int i = 0; i < 50; i++)
{
MyData.Add(new Data { Name = i.ToString(), Img = "/Background.png" });
}
}
, 나는에서 OutOfMemoryException을 얻을.
하지만 목록 상자 스타일을 설정하지 않은 경우 ListBox에 항목을 추가하면 프로젝트가 작동합니다.
오. 내 ListBox에 이미지가 있어야합니다. ListBox를 retemplate하고 데이터 가상화를 잃지 않는 방법을 알려주시겠습니까? – BillyMadisonnn
당신이 할 수있는 방법 중 하나 : "더 많은"버튼을 삭제하고 다음 항목을로드하기 위해 하단 압축 상태를 감지하십시오 – Ku6opr
솔루션 사용자 인 Scrollviewer와 itemsControl을 사용하여 ListBox를 구성하십시오 – BillyMadisonnn