【问题标题】:How to improve speed of adding controls to UI如何提高向 UI 添加控件的速度
【发布时间】:2013-07-07 08:12:13
【问题描述】:

我有以下代码创建 WPF 控件,然后以我需要的方式将它们添加到窗口中。它工作得很好,但是当尝试创建 256(x4 - 两个文本块、组合框、文本框)控件时,显示选项卡需要一段时间。窗口加载正常,但我有很多选项卡,当我单击点设置选项卡时,它会在显示选项卡之前稍微滞后。它只在我第一次单击选项卡时滞后,每次在第一次单击后它都会立即响应。

起初我认为这是一个渲染问题,但经过大量其他研究后,我觉得 C#/WPF 在动态创建一堆对象并将它们添加到表单方面做得不好。

如果我将项目数减少到 50,它会立即响应,100 是轻微滞后,而 200 (256) 则滞后一点,用户无法接受。

以前遇到过此类问题以及有关如何解决此问题或其他提示/技巧的建议。

提前致谢! 卫斯理

public static void pointSetup(VirtualizingStackPanel desc, VirtualizingStackPanel map) //Draws point description and point map table in point setup tab
    {
        StackPanel row;
        TextBlock text;
        TextBox textBox;
        ComboBox comboBox;

        Thickness rowSpacing = new Thickness(0, 0, 0, 5);
        Thickness textSpacing = new Thickness(0, 3, 5, 3);
        List<string> list = new List<string>();

        list.Add("xx");
        for (byte i = 0; i < Global.currentZonesToMap; i++)
        {
            list.Add("Zone  " + (i + 1));
        }

        for (short i = 0; i < 256; i++)
        {
            //desc
            row = new StackPanel();
            row.Margin = rowSpacing;
            row.Orientation = Orientation.Horizontal;

            text = new TextBlock();
            text.Text = "Point " + (i + 1);
            text.Margin = textSpacing;
            text.Width = 50;

            textBox = new TextBox();
            textBox.MaxLength = 28;
            textBox.Text = "";
            textBox.Width = 270;

            row.Children.Add(text);
            row.Children.Add(textBox);

            desc.Children.Add(row);

            //map
            row = new StackPanel();
            row.Margin = rowSpacing;
            row.Orientation = Orientation.Horizontal;

            text = new TextBlock();
            text.Text = "Point " + (i + 1);
            text.Margin = textSpacing;
            text.Width = 50;

            comboBox = new ComboBox();
            comboBox.ItemsSource = list;
            comboBox.Width = 270;

            row.Children.Add(text);
            row.Children.Add(comboBox);

            map.Children.Add(row);
        }
    }

新代码(使用 DataTemplate 和 ItemsControl)

    public class DevicePoint
    {
        public string desc { get; set; }

        public int zone { get; set; }

        public List<string> zones { get; set; }
    }

    //Initialized all variables and displays UI (constructor)
    public Dispatcher()
    {
        InitializeComponent();

        List<string> opts = new List<string>();
        opts.Add("xx");
        for (byte i = 0; i < Global.currentZonesToMap; i++)
        {
            opts.Add("Zone  " + (i + 1));
        }

        List<DevicePoint> points = new List<DevicePoint>();
        for (short i = 0; i < 256; i++)
            points.Add(new DevicePoint() { desc = "Point " + (i + 1), zone = 0, zones = opts });
        pointDesc.ItemsSource = points;
        pointZoneMap.ItemsSource = points;

        ... other stuff here ...
    }

        <StackPanel>
                <TextBlock Margin="10" FontWeight="Bold" HorizontalAlignment="Center" Text="Point Descriptions" />
                <ScrollViewer Width="360" Margin="30,10,30,10" MaxHeight="405">
                    <ItemsControl Name="pointDesc" Margin="5">
                        <ItemsControl.ItemTemplate>
                            <DataTemplate>
                                <StackPanel VirtualizingStackPanel.IsVirtualizing="True" Margin="0,0,0,5" Orientation="Horizontal">
                                    <TextBlock Margin="0,3,5,3" Width="50" Text="{Binding desc}" />
                                    <TextBox MaxLength="28" Width="270" Text="{Binding desc}" />
                                </StackPanel>
                            </DataTemplate>
                        </ItemsControl.ItemTemplate>
                    </ItemsControl>
                </ScrollViewer>
            </StackPanel>
            <StackPanel Grid.Column="1">
                <TextBlock Margin="10" FontWeight="Bold" HorizontalAlignment="Center" Text="Point - Zone Map" />
                <ScrollViewer Width="360" Margin="30,10,30,10" MaxHeight="405">
                    <ItemsControl Name="pointZoneMap" Margin="5">
                        <ItemsControl.ItemTemplate>
                            <DataTemplate>
                                <StackPanel VirtualizingStackPanel.IsVirtualizing="True" Margin="0,0,0,5" Orientation="Horizontal">
                                    <TextBlock Margin="0,3,5,3" Width="50" Text="{Binding desc}" />
                                    <ComboBox Width="270" ItemsSource="{Binding zones}" SelectedIndex="{Binding zone}" />
                                </StackPanel>
                            </DataTemplate>
                        </ItemsControl.ItemTemplate>
                    </ItemsControl>
                </ScrollViewer>
            </StackPanel>

【问题讨论】:

  • 请注意,通过使用代码创建对象,您实际上否定了有助于克服一次创建大量控件对性能造成的影响的虚拟化系统。我建议重组此代码以使用 ItemTemplate 和 ItemTemplate,而不是使用直接通过代码添加控件的旧 WinForms 方法。
  • “父”控件上可能有一些名为BeginUpdate 的方法会暂停更新UI,直到您调用EndUpdate,但我不是100% 确定。
  • 您还明确采用了 VirtualizingStackPanels,但随后通过直接添加项目来阻止它们进行任何虚拟化。
  • 作为辅助说明,ItemsControl 本身并不支持虚拟化(它主要是为ListBox 添加的。)您仍然可以使用here 描述的技术获得所需的行为。跨度>
  • @WesleyCarlsen,这是个大话题; this article 稍微介绍了 WPF 内容模型及其使用方式。一旦你开始了解 DataTemplates,其余的就会到位。

标签: c# wpf performance user-interface rendering


【解决方案1】:
  1. 如果您的计算机有多个内核,我假设它有,请尝试并行执行 for 循环(parallelfor(来自 .net 4 或更高版本)。
  2. 您可以在创建过程中将点列表大小设置为 256,这将防止在添加项目时分配内存。
  3. 如果 Global.currentZonesToMap 很大,请考虑使用 StringBuilder。
  4. 使用 StringBuilder 构建 DevicePoint.desc 字符串属性的值。

祝你好运,

M.摩西

【讨论】:

  • 不幸的是,所有控件都需要在UI线程上添加,这意味着Parallel已经出局了。我也怀疑列表后备存储的重新分配是否明显,并且 StringBuilder 实际上可能比在这种情况下隐式生成的 string.Concat 调用慢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-20
  • 2014-12-10
  • 2014-12-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多