wpf Grid 和 StackPanel,哪个性能更好?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15575854/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Grid and StackPanel, which has the better performance?
提问by Eddie
Let's read these codes, I've defined two similar UserControls in a Windows Phone 8 project and I really want to which of them is better. I've check the profiling, it seems they are almost the same.
让我们阅读这些代码,我在 Windows Phone 8 项目中定义了两个类似的 UserControl,我真的很想知道它们中的哪个更好。我检查了分析,似乎它们几乎相同。
UserControl 1, using grid's property to design my layout.
UserControl 1、使用grid的属性来设计我的布局。
<Grid x:Name="LayoutRoot" Background="{StaticResource PhoneChromeBrush}" Height="108">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Rectangle Grid.RowSpan="2" Grid.Row="0" Height="108" Width="54" Fill="Blue"></Rectangle>
<TextBlock Grid.Row="0" Grid.Column="1" Text="Caption" Style="{StaticResource PhoneTextExtraLargeStyle}"></TextBlock>
<TextBlock Grid.Row="1" Grid.Column="1" Text="URLURLURLURLURLURL" Style="{StaticResource PhoneTextSmallStyle}"></TextBlock>
</Grid>
UserControl 2, using StackPanel to design my layout.
UserControl 2、使用StackPanel来设计我的布局。
<Grid x:Name="LayoutRoot" Background="{StaticResource PhoneChromeBrush}" Height="108">
<StackPanel Orientation="Horizontal">
<Rectangle Height="108" Width="54" Fill="Red"></Rectangle>
<StackPanel Orientation="Vertical">
<TextBlock Text="Caption" Style="{StaticResource PhoneTextExtraLargeStyle}"></TextBlock>
<TextBlock Text="URLURLURLURLURLURL" Style="{StaticResource PhoneTextSmallStyle}"></TextBlock>
</StackPanel>
</StackPanel>
</Grid>
It looks like the basic layout is the same. But when I use XAML Spy to analyse the Visualizing tree, UserControl 1 has less nodes, but it costs a little more memory. Why?
看起来基本布局是一样的。但是当我使用 XAML Spy 来分析可视化树时,UserControl 1 的节点较少,但它需要更多的内存。为什么?
回答by Rachel
You may be interested in the answers to this question: In what order are Panels the most efficient in terms of render time and performance?
您可能对这个问题的答案感兴趣:在渲染时间和性能方面,面板以什么顺序最有效?
The short answer is it depends on how many children the panels have, and how those elements are sized and positioned. But in most cases, a StackPanelwill be more efficient than a Gridas it has both a faster measure and arrangement pass.
简短的回答是,这取决于面板有多少个子元素,以及这些元素的大小和位置。但在大多数情况下,aStackPanel将比 a 更有效,Grid因为它具有更快的测量和排列通道。
To quote from the accepted answer:
引用已接受的答案:
Grid
Defines a flexible grid area that consists of columns and rows.
This can be the most performance intensive panel if proportional sizing or auto sizing is used. Calculating child item size can be a complex combination of the native size of the item and the layout specified by the grid. Layout is also the most complicated of all the panels. Slow to medium performance for the measure pass and slow to medium performance for the arrangement pass.
StackPanel
Arranges child elements into a single line that can be oriented horizontally or vertically.
The StackPanel measures its children using either native or relative sizing in the opposite direction from its orientation and native sizing in the direction of its orientation (alignment does nothing in this direction). This makes it a mid-level performer in this area. The Arrangement pass is simply, just laying out the items in order. Probably the second-best performance for this pass. Medium performance for the measure pass and fast performance for the layout pass.
网格
定义由列和行组成的灵活网格区域。
如果使用比例调整大小或自动调整大小,这可能是性能最密集的面板。计算子项大小可以是项的本机大小和网格指定的布局的复杂组合。布局也是所有面板中最复杂的。测量通过的慢到中等性能和排列通过的慢到中等性能。
堆栈面板
将子元素排列成可以水平或垂直定向的单行。
StackPanel 使用与其方向相反方向的本地或相对大小和在其方向的方向上的本地大小来测量其子项(对齐在这个方向上没有任何作用)。这使其成为该领域的中级表现者。安排通行证很简单,只是按顺序排列项目。可能是这次传球的第二好的表现。测量通过的中等性能和布局通过的快速性能。
Also in regards to memory consumption, both objects are different and take up different amounts of memory, and a Gridhas RowDefinitionsand ColumnDefinitions, so it actually contains more objects than your StackPanel
另外在内存消耗方面,两个对象都不同,占用的内存量也不同,并且Grid有RowDefinitions和ColumnDefinitions,所以它实际上包含的对象比你的多StackPanel

