Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 网格和StackPanel,哪个性能更好?_Wpf_Windows_Windows Phone 8 - Fatal编程技术网

Wpf 网格和StackPanel,哪个性能更好?

Wpf 网格和StackPanel,哪个性能更好?,wpf,windows,windows-phone-8,Wpf,Windows,Windows Phone 8,让我们读一下这些代码,我在WindowsPhone8项目中定义了两个类似的用户控件,我真的想知道哪一个更好。我检查过剖面图,看起来几乎一样 UserControl 1,使用网格的属性设计我的布局 <Grid x:Name="LayoutRoot" Background="{StaticResource PhoneChromeBrush}" Height="108"> <Grid.RowDefinitions> <RowDefinition H

让我们读一下这些代码,我在WindowsPhone8项目中定义了两个类似的用户控件,我真的想知道哪一个更好。我检查过剖面图,看起来几乎一样

UserControl 1,使用网格的属性设计我的布局

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

UserControl 2,使用StackPanel设计我的布局

<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>
<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的节点较少,但占用的内存稍多一些。为什么?

您可能对这个问题的答案感兴趣:

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

引自:

网格

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

如果符合比例,这可能是性能最密集的面板 使用大小调整或自动大小调整。计算子项大小可以是 项目的本机大小和布局的复杂组合 由网格指定。布局也是最复杂的 面板。测量通过的性能从慢到中等,测量通过的性能从慢到中等 安排通行证的中等性能

StackPanel

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

StackPanel使用本机或相对方式测量其子级 按与其方向和本机方向相反的方向调整大小 在其方向上调整大小(对齐在 这个方向)。这使其成为该领域的中级执行者。这个 安排通行证很简单,只需将项目按顺序排列即可。 可能是这次传球第二好的表现。中等性能 用于测量过程和布局过程的快速性能


同样在内存消耗方面,两个对象都不同,占用的内存量也不同,
网格
具有
行定义
列定义
,因此它实际上包含的对象比您的
堆栈面板

可能的重复显然是重复的,被接受的答案甚至只是链接并引用了另一个问题的答案。