WPF列表框项目消耗太多内存

时间:2013-08-11 11:44:03

标签: c# wpf memory listbox listboxitems

好的,我有一个WPF应用程序,我在资源字典中创建自己的ListBoxItem,如下所示:

    <DataTemplate x:Key="StationItem">
    <Grid x:Name="Gridder" Tag="{Binding SItem.StationName}" Width="125" Height="55">
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="20"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="20"/>
        </Grid.ColumnDefinitions>
        <Rectangle x:Name="Rectagler" Width="120" Height="50" RadiusX="5" RadiusY="5" VerticalAlignment="Center" HorizontalAlignment="Center" Grid.ColumnSpan="2" Grid.RowSpan="2" StrokeThickness="1" Stroke="Black">
            <Rectangle.Fill>
                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                    <GradientStop Color="#FFFF3A00" Offset="0"/>
                    <GradientStop Color="#FFE88D20" Offset="1"/>
                </LinearGradientBrush>
            </Rectangle.Fill>
        </Rectangle>
        <TextBlock Grid.Column="0" Grid.Row="0" Grid.ColumnSpan="2" x:Name="StationName" Text="{Binding SItem.StationName}" Foreground="Black" FontSize="14" FontWeight="SemiBold" TextWrapping="NoWrap" VerticalAlignment="Center" HorizontalAlignment="Left" Margin="10,0,0,0"/>
        <TextBlock Grid.Column="0" Grid.Row="1" x:Name="StationCountry" Text="{Binding SItem.StationCountry}" Foreground="WhiteSmoke" FontSize="11" FontWeight="Light" TextWrapping="NoWrap" VerticalAlignment="Center" HorizontalAlignment="Left" Margin="10,0,0,8"/>
        <Image Tag="{Binding SItem.StationName}" Width="15" Height="15" RenderOptions.BitmapScalingMode="HighQuality" Grid.Column="1" Grid.Row="1" Source="{Binding FavSource}" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="0,0,10,10"/>
    </Grid>
    <DataTemplate.Triggers>
        <Trigger Property="IsMouseOver" Value="true">
            <Setter TargetName="Rectagler" Property="Width" Value="125"/>
            <Setter TargetName="Rectagler" Property="Height" Value="55"/>
            <Setter TargetName="StationName" Property="Foreground" Value="White"/>
        </Trigger>
    </DataTemplate.Triggers>
</DataTemplate>

这样的ListBox

        <ListBox x:Name="StationsListLB" Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="5" ItemTemplate="{DynamicResource GenreStyle}" ItemContainerStyle="{DynamicResource ContainerStyle}" Background="{x:Null}" BorderBrush="{x:Null}" PreviewMouseLeftButtonDown="StationsListLB_PreviewMouseLeftButtonDown" ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.VerticalScrollBarVisibility="Disabled" PreviewMouseDoubleClick="StationsListLB_PreviewMouseDoubleClick">
        <ListBox.ItemsPanel>
            <ItemsPanelTemplate>
                <WrapPanel IsItemsHost="True" Orientation="Vertical" MaxHeight="220" ScrollViewer.HorizontalScrollBarVisibility="Disabled" ScrollViewer.VerticalScrollBarVisibility="Disabled" HorizontalAlignment="Center" VerticalAlignment="Center"/>
            </ItemsPanelTemplate>
        </ListBox.ItemsPanel>
    </ListBox>

当用户选择电台的类型时,我会填充ListBox个项目,方法如下:

 public class Stations
{
    public StationItem SItem { get; set; }
    public string FavSource { get; set; }
}

public class StationItem
{
    public string StationName { get; set; }
    public string StationUrl { get; set; }
    public string StationGenre { get; set; }
    public string StationWebsite { get; set; }
    public string StationCountry { get; set; }
}

    private void LoadStationByGenre(string Genre)
    {
        StationsListLB.Items.Clear();
        StationsListLB.ItemTemplate = (DataTemplate)FindResource("StationItem");

        IEnumerable<Stations> results = StationList.Where(s => s.SItem.StationGenre.Equals(Genre));

        foreach (var Station in results)
        {
            StationsListLB.Items.Add(Station);
        }
    }

注意: 每个StationGenre都会填充ListBox至少1000 StationItems

我的代码工作正常,内存为40MB( Window任务管理器)。但每当我选择一个类型并填充我的ListBox时,内存会以100MB 爆炸。然后我选择另一个类型内存进一步爆炸。我认为ListBox.Items.Clear(); ListBoxItems也会从记忆中摧毁,但这不会发生。

我尝试从我的ListoBoxItemTemplate删除图像并检查我的应用程序的内存,但没有看到真正的区别(大约4-7 MB的区别)。

所以我的回答是:在ListBox中添加项目时,如何让我的应用程序消耗更少的内存?

提前致谢!!

1 个答案:

答案 0 :(得分:2)

尝试使用VirtualizingStackPanel.VirtualizationMode="Recycling"来提高效果。引自MSDN

  

默认情况下,VirtualizingStackPanel为每个可见项创建一个项容器,并在不再需要时丢弃它(例如当项滚动到视图外时)。当ItemsControl包含大量项目时,创建和丢弃项目容器的过程可能会对性能产生负面影响。当VirtualizingStackPanel.VirtualizationMode设置为Recycling时,VirtualizingStackPanel会重复使用项容器,而不是每次都创建一个新容器。