繁体   English   中英

如何提高向UI添加控件的速度

[英]How to improve speed of adding controls to UI

我有以下代码创建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>
  1. 如果您的计算机具有多个核心,并且我假设它具有多个核心,请尝试并行执行for循环(parallelfor(来自.net 4或更高版本)。
  2. 您可以将创建期间的点列表大小设置为256,这将防止在项目添加操作期间分配内存。
  3. 如果Global.currentZonesToMap大,请考虑使用StringBuilder。
  4. 使用StringBuilder生成DevicePoint.desc字符串属性的值。

祝好运,

莫西

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM