ItemsControl和DataBinding的内存问题

时间:2017-01-10 17:31:01

标签: c# .net wpf data-binding

我正在为我的WPF应用程序开发一个调试视图,它在ItemsControl中打印了大量数据(数千个项目)。

每个项目都非常简单,如下所示:

<ScrollViewer VerticalScrollBarVisibility="Auto">
    <StackPanel Orientation="Vertical">
      <ItemsControl ItemsSource="{Binding Frames,
                                          Mode=OneTime}"
                    VirtualizingStackPanel.IsVirtualizing="True"
                    VirtualizingStackPanel.VirtualizationMode="Recycling">
          <ItemsControl.ItemsPanel>
              <ItemsPanelTemplate>
                  <VirtualizingStackPanel IsItemsHost="True" />
              </ItemsPanelTemplate>
          </ItemsControl.ItemsPanel>
          <ItemsControl.ItemTemplate>
              <DataTemplate>
                  <Grid Margin="50,0,0,0">
                      <Grid.RowDefinitions>
                          <RowDefinition Height="25" />
                          <RowDefinition Height="25" />
                          <RowDefinition Height="25" />
                          <RowDefinition Height="25" />
                          <RowDefinition Height="25" />
                          <RowDefinition Height="25" />
                      </Grid.RowDefinitions>
                      <Grid.ColumnDefinitions>
                          <ColumnDefinition />
                          <ColumnDefinition />
                      </Grid.ColumnDefinitions>
                      <StackPanel Grid.Row="0"
                                  Grid.Column="0"
                                  Orientation="Horizontal">
                          <TextBlock FontSize="16" Text="Date (HH:mm::ss::FFF)" />
                          <TextBlock Margin="5,0,5,0"
                                     FontSize="16"
                                     Text="|" />
                          <TextBlock FontSize="16" Text="Frame number" />
                      </StackPanel>
                      <StackPanel Grid.Row="0"
                                  Grid.Column="1"
                                  Orientation="Horizontal">
                          <TextBlock FontSize="16"
                                     Foreground="{StaticResource BackgroundSelected}"
                                     Text="{Binding date,
                                                    Converter={StaticResource DateTimeToFullDateConverter}}" />
                          <TextBlock Margin="5,0,5,0"
                                     FontSize="16"
                                     Text="|" />
                          <TextBlock FontSize="16"
                                     Foreground="{StaticResource BackgroundSelected}"
                                     Text="{Binding frameNumber}" />
                      </StackPanel>

                      <StackPanel Grid.Row="1"
                                  Grid.Column="0"
                                  Orientation="Horizontal">
                          <TextBlock FontSize="16" Text="Framerate (in fps)" />
                          <TextBlock Margin="5,0,5,0"
                                     FontSize="16"
                                     Text="|" />
                          <TextBlock FontSize="16" Text="Bitrate (in kB/s)" />
                      </StackPanel>
                      <StackPanel Grid.Row="1"
                                  Grid.Column="1"
                                  Orientation="Horizontal">
                          <TextBlock FontSize="16"
                                     Foreground="{StaticResource BackgroundSelected}"
                                     Text="{Binding framerate}" />
                          <TextBlock Margin="5,0,5,0"
                                     FontSize="16"
                                     Text="|" />
                          <TextBlock FontSize="16"
                                     Foreground="{StaticResource BackgroundSelected}"
                                     Text="{Binding bitrate}" />
                      </StackPanel>
                      <StackPanel Grid.Row="2"
                                  Grid.Column="0"
                                  Orientation="Horizontal">
                          <TextBlock FontSize="16" Text="Width (in pixels)" />
                          <TextBlock Margin="5,0,5,0"
                                     FontSize="16"
                                     Text="|" />
                          <TextBlock FontSize="16" Text="Height (in pixels)" />
                      </StackPanel>

                      <StackPanel Grid.Row="2"
                                  Grid.Column="1"
                                  Orientation="Horizontal">
                          <TextBlock FontSize="16"
                                     Foreground="{StaticResource BackgroundSelected}"
                                     Text="{Binding width}" />
                          <TextBlock Margin="5,0,5,0"
                                     FontSize="16"
                                     Text="|" />
                          <TextBlock FontSize="16"
                                     Foreground="{StaticResource BackgroundSelected}"
                                     Text="{Binding height}" />
                      </StackPanel>
                      <TextBlock Grid.Row="3"
                                 Grid.Column="0"
                                 FontSize="16"
                                 Text="Delay (in milliseconds)" />
                      <TextBlock Grid.Row="3"
                                 Grid.Column="1"
                                 FontSize="16"
                                 Foreground="{StaticResource BackgroundSelected}"
                                 Text="{Binding delay}" />

                      <TextBlock Grid.Row="4"
                                 Grid.Column="0"
                                 FontSize="16"
                                 Text="Type" />
                      <TextBlock Grid.Row="4"
                                 Grid.Column="1"
                                 FontSize="16"
                                 Foreground="{StaticResource BackgroundSelected}"
                                 Text="{Binding type}" />
                      <Separator Grid.Row="5"
                                 Grid.Column="0"
                                 Grid.ColumnSpan="2"
                                 Margin="5" />
                  </Grid>
              </DataTemplate>
          </ItemsControl.ItemTemplate>
      </ItemsControl>
   </StackPanel>
</ScrollViewer>

My Frames属性绑定到ObservableCollection,我每秒调用Add方法60次。

问题是,一旦集合包含数百个项目(从250MB开始并在30秒内升至700MB +),我的内存消耗就会跳跃,而且我的CPU使用率也非常高......

我知道WPF遇到的性能问题,但我想知道是否可以在我的控件中显示数千个项目而不会崩溃我的应用程序。

Screenshot

0 个答案:

没有答案