Grid 和 StackPanel,哪个性能更好? [英] Grid and StackPanel, which has the better performance?

查看:36
本文介绍了Grid 和 StackPanel,哪个性能更好?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们阅读这些代码,我在 Windows Phone 8 项目中定义了两个类似的 UserControl,我真的很想知道它们中的哪个更好.我检查了分析,它们似乎几乎相同.

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.

UserControl 1,使用 grid 的属性来设计我的布局.

UserControl 1, using grid's property to design my layout.

<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,使用 StackPanel 来设计我的布局.

UserControl 2, using StackPanel to design my layout.

<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>

看起来基本布局是一样的.但是当我使用 XAML Spy 来分析可视化树时,UserControl 1 的节点较少,但它需要更多的内存.为什么?

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?

推荐答案

您可能对这个问题的答案感兴趣:在什么就渲染时间和性能而言,Panel 的顺序是最有效的吗?

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?

简而言之,这取决于面板有多少个子元素,以及这些元素的大小和位置.但在大多数情况下,StackPanel 将比 Grid 更有效,因为它具有更快的测量和排列传递.

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 StackPanel will be more efficient than a Grid as it has both a faster measure and arrangement pass.

引用接受的答案:

网格

定义由列和行组成的灵活网格区域.

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.

堆栈面板

将子元素排列成一行,可以水平或垂直方向.

Arranges child elements into a single line that can be oriented horizontally or vertically.

StackPanel 使用本地或相对测量其子项在与其方向和本机相反的方向上调整大小在其方向的方向上调整大小(对齐在这个方向).这使其成为该领域的中级表现者.这安排关很简单,就是把物品按顺序排列好.可能是这次传球的第二好的表现.中等性能用于测量通过和布局通过的快速性能.

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.

另外关于内存消耗,两个对象不同,占用的内存量也不同,GridRowDefinitionsColumnDefinitions,所以它实际上包含比你的 StackPanel

Also in regards to memory consumption, both objects are different and take up different amounts of memory, and a Grid has RowDefinitions and ColumnDefinitions, so it actually contains more objects than your StackPanel

这篇关于Grid 和 StackPanel,哪个性能更好?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆