【问题标题】:Minimizing memory consumption listbox of images (WPF)最小化图像的内存消耗列表框(WPF)
【发布时间】:2013-01-25 16:19:00
【问题描述】:

我有绑定到 ObservableCollection 并使用文件名显示图像的列表框

我的 xaml 是:

<Window x:Class="ThumbnailsView.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="578" WindowStartupLocation="CenterScreen">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="55"/>
        </Grid.RowDefinitions>

            <ListBox Grid.Row="0" x:Name="ImageListbox"
        ItemsSource="{Binding}" 
        Background="AliceBlue" ScrollViewer.HorizontalScrollBarVisibility="Disabled">

                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <CheckBox Height="16" VerticalAlignment="Top" Margin="0,10,0,0"/>
                            <Image Margin="10,10,10,0" Height="64" Width="64" VerticalAlignment="Top">
                                <Image.Source>
                                    <BitmapImage DecodePixelWidth="64" UriSource="{Binding Path=Name}"/>
                                </Image.Source>                            
                            </Image>
                    </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>

        <ListBox.ItemsPanel>
                    <ItemsPanelTemplate>
                        <WrapPanel/>
                    </ItemsPanelTemplate>
                </ListBox.ItemsPanel>
            </ListBox>

            <Button Grid.Row="1" Content="Get Images" Name="getImageBtn" Click="getImageBtn_Click" Width="100" Height="30"></Button>

    </Grid>
</Window>

问题是,它会加载整个图像并且如果我有大量的集合会消耗大量内存。如何最小化它的内存消耗?

【问题讨论】:

  • 一种解决方案是对滚动行为进行延迟加载,同时处理您传递的项目。简而言之,只需加载您当前可以在表单面板上看到的项目。

标签: c# wpf listbox


【解决方案1】:

【讨论】:

  • 对不起,我是 c# 和 wpf 的新手。你能指出我的示例代码吗?
  • +1,但如果没有some extra effort,他无法虚拟化 WrapPanel。
【解决方案2】:

加载图像后,将其调整为更易于管理的大小,然后释放未使用的大图像。这仍然需要很长时间才能加载,但会占用更少的内存。要减少加载时间,请参阅 dutzu's 回答并使用延迟加载和虚拟化。

【讨论】:

    【解决方案3】:

    使用虚拟化堆栈面板 查看链接以获取有关如何执行此操作的示例。

    http://www.jonathanantoine.com/2011/10/07/wpf-4-5-%E2%80%93-part-11-new-features-for-the-virtualizingpanel/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-12
      • 2017-09-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-30
      • 2012-11-01
      相关资源
      最近更新 更多