Panel performance
程序:
<StackPanel HorizontalAlignment="Left">
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
<TextBlock Text="Field 1: " VerticalAlignment="Center"/>
<TextBox Width="100"/>
</StackPanel>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
<TextBlock Text="Field 2: " VerticalAlignment="Center"/>
<TextBox Width="100"/>
</StackPanel>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
<TextBlock Text="Longer Field 3: " VerticalAlignment="Center"/>
<TextBox Width="100"/>
</StackPanel>
</StackPanel>
有四个StackPanel意味着要执行四次measurement和arrangement,第一个父StackPanel让自己的三个孩子StackPanel执行measurement和arragement,这样性能损失严重,修改为:
Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="100"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<TextBlock Text="Field 1: "
Grid.Column="0"
Grid.Row="0"
HorizontalAlignment="Right"
VerticalAlignment="Center"/>
<TextBox Grid.Column="1"
Grid.Row="0"/>
<TextBlock Text="Field 2: "
Grid.Column="0"
Grid.Row="1"
HorizontalAlignment="Right"
VerticalAlignment="Center"/>
<TextBox Grid.Column="1"
Grid.Row="1"/>
<TextBlock Text="Longer Field 3: "
Grid.Column="0"
Grid.Row="2"
HorizontalAlignment="Right"
VerticalAlignment="Center"/>
<TextBox Grid.Column="1"
Grid.Row="2"/>
</Grid>
这样只有一个panel,只需要执行一次measurement 和 arrangement计算。