WPF ListboxItems占用的内存太多

本文关键字:内存 太多 ListboxItems WPF | 更新日期: 2023-09-27 18:27:08

好的,我有一个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都会用至少1000个StationItems填充我的ListBox

我的代码运行良好,以40MB的内存开始(窗口任务管理器)。但每当我选择一种类型并填充我的ListBox时,内存会以100MB的速度爆炸。然后我选择另一种类型的内存会进一步爆炸。我以为有了ListBox.Items.Clear();ListBoxItems也会从内存中被破坏,但这并没有发生。

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

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

提前感谢!!

WPF ListboxItems占用的内存太多

尝试使用VirtualizingStackPanel.VirtualizationMode="Recycling"来提高性能。MSDN报价:

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