none
请问什么叫做“Top-down”? RRS feed

  • 问题

  • 无意中在微软网页上看到的,说又例如提高WPF的性能:

    Build Your Tree Top-Down

    When a node is added or removed from the logical tree, property invalidations are raised on the node's parent and all its children. As a result, a top-down construction pattern should always be followed to avoid the cost of unnecessary invalidations on nodes that have already been validated. The following table shows the difference in execution speed between building a tree top-down versus bottom-up, where the tree is 150 levels deep with a single TextBlock and DockPanel at each level.

    那请问什么叫Top——Down?

    引用:

    https://docs.microsoft.com/en-us/dotnet/framework/wpf/advanced/optimizing-performance-layout-and-design


    da jia hao!

    2017年9月18日 7:03

答案


  • Hi liubin,
        
    >>请问什么叫Top——Down?

    从官方文档上的代码意思就是你在建立tree节点的时候,遵循从增加第一个父节,然后添加他的子节点,子子节点这样的模式。官网文档中提供的代码例子就是如此。

    private void OnBuildTreeTopDown(object sender, RoutedEventArgs e)
    {
        TextBlock textBlock = new TextBlock();
        textBlock.Text = "Default";
    
        DockPanel parentPanel = new DockPanel();
        DockPanel childPanel;
    
        myCanvas.Children.Add(parentPanel);
        myCanvas.Children.Add(textBlock);
    
        for (int i = 0; i < 150; i++)
        {
            textBlock = new TextBlock();
            textBlock.Text = "Default";
            parentPanel.Children.Add(textBlock);
    
            childPanel = new DockPanel();
            parentPanel.Children.Add(childPanel);
            parentPanel = childPanel;
        }
    }

    如果您先添加子元素,然后添加父元素,然后添加其父元素等,则会导致主要的性能成本,官方文档也有此说明,并有数据给你参考。

    Trees in WPF:
    https://docs.microsoft.com/ZH-CN/dotnet/framework/wpf/advanced/trees-in-wpf
     
    Optimizing Performance: Layout and Design
    https://docs.microsoft.com/en-us/dotnet/framework/wpf/advanced/optimizing-performance-layout-and-design


    Best Regards,

    Yohann Lu



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • 已标记为答案 liubin 2017年9月20日 7:16
    2017年9月19日 5:47
    版主