我有UserControl
这是一个项目列表,其中每个项目都包含大量TextBox
和TextBlock
控件。我尝试禁用撤消步骤,但即使在列表中添加了10个项目,内存消耗也非常高。我尝试运行内存分析工具,我可以看到TextBoxes是大约50 MB内存的最大消费者。
我的XAML如下
<UserControl.Resources>
<converter:GenericButtonConverter x:Key="ButtonConverter"/>
<DataTemplate x:Key="RawSendListTemplate">
<ListBoxItem BorderBrush="LightGray" BorderThickness="0.5" HorizontalAlignment="Stretch" Margin="3,1,3,1">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="52"/>
<ColumnDefinition Width="80"/>
<ColumnDefinition Width="70"/>
<ColumnDefinition Width="40"/>
<ColumnDefinition Width="40"/>
<ColumnDefinition Width="40"/>
<ColumnDefinition Width="80"/>
<ColumnDefinition Width="60"/>
<ColumnDefinition Width="40"/>
<ColumnDefinition Width="60"/>
<ColumnDefinition Width="45"/>
<ColumnDefinition Width="36"/>
<ColumnDefinition Width="36"/>
<ColumnDefinition Width="36"/>
<ColumnDefinition Width="36"/>
<ColumnDefinition Width="36"/>
<ColumnDefinition Width="36"/>
<ColumnDefinition Width="36"/>
<ColumnDefinition Width="36"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Text="ID" MinWidth="20" FontSize="11" FontWeight="Bold" Foreground="Coral"
VerticalAlignment="Center" TextAlignment="Center" Margin="0,0,5,0" />
<TextBox Grid.Column="1" Text="{Binding Path=CanId, UpdateSourceTrigger=PropertyChanged}" MinWidth="20" FontSize="11" Foreground="Blue" FontWeight="SemiBold" TextWrapping="NoWrap" MaxLength="8"
VerticalAlignment="Center" HorizontalContentAlignment="Center" Margin="0,0,15,0" UndoLimit="0" AcceptsReturn="False" IsUndoEnabled="False" MaxLines="1" />
<TextBlock Grid.Column="2" Text="CHANNEL" MinWidth="20" FontSize="11" FontWeight="Bold" Foreground="Coral"
VerticalAlignment="Center" TextAlignment="Center" Margin="0,0,5,0" />
<TextBox Grid.Column="3" Text="{Binding Path=Channel, UpdateSourceTrigger=PropertyChanged}" MinWidth="20" FontSize="11" Foreground="Blue" FontWeight="SemiBold" TextWrapping="NoWrap" MaxLength="1"
VerticalAlignment="Center" HorizontalContentAlignment="Center" Margin="0,0,15,0" UndoLimit="0" AcceptsReturn="False" IsUndoEnabled="False" MaxLines="1"/>
<TextBlock Grid.Column="4" Text="Length" MinWidth="20" FontSize="11" FontWeight="Bold" Foreground="Coral"
VerticalAlignment="Center" TextAlignment="Center" Margin="0,0,5,0" />
<TextBox Grid.Column="5" Text="{Binding Path=Len , UpdateSourceTrigger=PropertyChanged}" MinWidth="20" FontSize="11" Foreground="Blue" FontWeight="SemiBold" MaxLength="1"
VerticalAlignment="Center" HorizontalContentAlignment="Center" Margin="0,0,15,0" UndoLimit="0" AcceptsReturn="False" IsUndoEnabled="False" MaxLines="1"/>
<Button Grid.Column="6" Content="Send" x:Name="RawSendButton" IsEnabled="{Binding Path=SendButtonEnabled}" MinWidth="20" FontSize="11" FontWeight="SemiBold" Foreground="Black"
VerticalAlignment="Center" HorizontalContentAlignment="Center" Margin="0,0,5,0"
Command="{Binding ButtonClick}" CommandParameter="{Binding RelativeSource={RelativeSource Self}, Path=Name, Converter={StaticResource ButtonConverter}}"/>
<TextBlock Grid.Column="7" Text="CYCLE" MinWidth="20" FontSize="11" FontWeight="Bold" Foreground="Coral"
VerticalAlignment="Center" TextAlignment="Center" Margin="0,0,5,0" />
<TextBox Grid.Column="8" Text="{Binding Path=CycleTimeMs, UpdateSourceTrigger=PropertyChanged}" IsEnabled="{Binding Path=SendButtonEnabled}" MinWidth="20" FontSize="11" Foreground="Blue" FontWeight="SemiBold" MaxLength="4"
VerticalAlignment="Center" TextAlignment="Center" HorizontalContentAlignment="Center" Margin="0,0,5,0" UndoLimit="0" AcceptsReturn="False" IsUndoEnabled="False" MaxLines="1"/>
<CheckBox Grid.Column="9" IsChecked="{Binding Path=CyclicEnableChecked, Mode=TwoWay}" IsEnabled="{Binding Path=CyclicEnable, UpdateSourceTrigger=PropertyChanged}" MinWidth="20"
VerticalAlignment="Center" HorizontalContentAlignment="Center" Margin="0,0,40,0"/>
<TextBlock Grid.Column="10" Text="DATA" MinWidth="20" FontSize="11" FontWeight="Bold" Foreground="Coral"
VerticalAlignment="Center" TextAlignment="Left" Margin="0,0,10,0" />
<TextBox Grid.Column="11" Text="{Binding Path=Byte1}" IsEnabled="{Binding Path=Byte1Enabled, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" MinWidth="20" FontSize="11" Foreground="Blue" FontWeight="SemiBold" MaxLength="2"
TextAlignment="Right" VerticalAlignment="Center" HorizontalContentAlignment="Center" Margin="0,0,5,0" UndoLimit="0" AcceptsReturn="False" IsUndoEnabled="False" MaxLines="1"/>
<TextBox Grid.Column="12" Text="{Binding Path=Byte2}" IsEnabled="{Binding Path=Byte2Enabled, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" MinWidth="20" FontSize="11" Foreground="Blue" FontWeight="SemiBold" MaxLength="2"
VerticalAlignment="Center" TextAlignment="Right" HorizontalContentAlignment="Center" Margin="0,0,5,0" UndoLimit="0" AcceptsReturn="False" IsUndoEnabled="False" MaxLines="1"/>
<TextBox Grid.Column="13" Text="{Binding Path=Byte3}" IsEnabled="{Binding Path=Byte3Enabled, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" MinWidth="20" FontSize="11" Foreground="Blue" FontWeight="SemiBold" MaxLength="2"
VerticalAlignment="Center" TextAlignment="Right" HorizontalContentAlignment="Center" Margin="0,0,5,0" UndoLimit="0" AcceptsReturn="False" IsUndoEnabled="False" MaxLines="1"/>
<TextBox Grid.Column="14" Text="{Binding Path=Byte4}" IsEnabled="{Binding Path=Byte4Enabled, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" MinWidth="20" FontSize="11" Foreground="Blue" FontWeight="SemiBold" MaxLength="2"
VerticalAlignment="Center" TextAlignment="Right" HorizontalContentAlignment="Center" Margin="0,0,5,0" UndoLimit="0" AcceptsReturn="False" IsUndoEnabled="False" MaxLines="1"/>
<TextBox Grid.Column="15" Text="{Binding Path=Byte5}" IsEnabled="{Binding Path=Byte5Enabled, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" MinWidth="20" FontSize="11" Foreground="Blue" FontWeight="SemiBold" MaxLength="2"
VerticalAlignment="Center" TextAlignment="Right" HorizontalContentAlignment="Center" Margin="0,0,5,0" UndoLimit="0" AcceptsReturn="False" IsUndoEnabled="False" MaxLines="1"/>
<TextBox Grid.Column="16" Text="{Binding Path=Byte6}" IsEnabled="{Binding Path=Byte6Enabled, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" MinWidth="20" FontSize="11" Foreground="Blue" FontWeight="SemiBold" MaxLength="2"
VerticalAlignment="Center" TextAlignment="Right" HorizontalContentAlignment="Center" Margin="0,0,5,0" UndoLimit="0" AcceptsReturn="False" IsUndoEnabled="False" MaxLines="1"/>
<TextBox Grid.Column="17" Text="{Binding Path=Byte7}" IsEnabled="{Binding Path=Byte7Enabled, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" MinWidth="20" FontSize="11" Foreground="Blue" FontWeight="SemiBold" MaxLength="2"
VerticalAlignment="Center" TextAlignment="Right" HorizontalContentAlignment="Center" Margin="0,0,5,0" UndoLimit="0" AcceptsReturn="False" IsUndoEnabled="False" MaxLines="1"/>
<TextBox Grid.Column="18" Text="{Binding Path=Byte8}" IsEnabled="{Binding Path=Byte8Enabled, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" MinWidth="20" FontSize="11" Foreground="Blue" FontWeight="SemiBold" MaxLength="2"
VerticalAlignment="Center" TextAlignment="Right" HorizontalContentAlignment="Center" Margin="0,0,5,0" UndoLimit="0" AcceptsReturn="False" IsUndoEnabled="False" MaxLines="1"/>
</Grid>
</ListBoxItem>
</DataTemplate>
</UserControl.Resources>
<Grid>
<ListBox x:Name="RawSendList" BorderThickness="1" BorderBrush="DarkGray" Margin="1" ItemsSource="{Binding MyViewModel.RawMessages}" ItemTemplate="{StaticResource RawSendListTemplate}"
SelectedItem="{Binding MyViewModel.SelectedRawMessageItem, Mode=OneWayToSource}" SelectionMode="Single" Background="OldLace"/>
</Grid>
我需要一些帮助来减少列表项的内存占用。
功能名称总CPU(%)自CPU(%)总CPU(ms)自CPU(ms)模块
Gazelle.exe(PID:8516)100.00%0.00%29761 0 Gazelle.exe
[外部代码] 99.80%23.01%29701 6849 50个模块
Gazelle.App::Main 0.19%0.00%58 0 Gazelle.exe
Gazelle.MainWindow :: InitializeComponent 0.01%0.00%2 0 Gazelle.exe
对象类型计数大小(字节)包含大小(字节)模块
ListItem 10,464 251,136 573,839,736 PresentationCore.dll
TextBox 10,008 18,376,324 278,265,656 PresentationFramework.dll
清单25,098 23,258,200 185,202,668 mscorlib.dll
TextEditor 10,008 2,175,548 158,375,060 PresentationFramework.dll
Grid 12,624 12,400,172 151,944,940 PresentationFramework.dll
ListBoxItem 1,668 1,013,444 128,034,940 PresentationFramework.dll
TemplateNameScope 25,098 803,136 121,480,864 PresentationFramework.dll
ScrollViewer 10,015 10,694,204 81,834,452 PresentationFramework.dll
ContentPresenter 3,387 2,420,780 28,512,660 PresentationFramework.dll
EventHandler 233,861 10,732,196 25,202,352 mscorlib.dll
BindingExpression 65,292 24,139,292 24,189,152 PresentationFramework.dll
Border 13,419 11,634,248 18,898,436 PresentationFramework.dll
TextBoxView 10,008 11,549,424 18,597,828 PresentationFramework.dll
HybridDictionary 52,843 7,811,596 18,090,272 System.dll
ModifiedValue 192,048 4,803,732 16,476,568 WindowsBase.dll
ScrollBar 20,022 15,381,092 16,358,688 PresentationFramework.dll
ScrollContentPresenter 10,011 10,158,248 14,169,188 PresentationFramework.dll
[UPDATE]
在XAML中添加它可以使应用程序更具响应性
UndoLimit="0" AcceptsReturn="False" IsUndoEnabled="False"
现在我想知道我还能在哪里获得更多