WPF - 用户控件非常慢
我正在设计类似 PropertyGrid 的东西,我想在其中显示对象的属性。由于特殊原因,我不会使用 PropertyGrid,而是创建我自己的。
对于每个属性,我都创建了一个自定义用户控件。现在令我恐惧的是,表现非常糟糕。如果我有大约 100 个属性,则需要 500 毫秒才能在 StackPanel/Listbox 中显示它们。
我做了一个实验,将 200 个默认用户控件添加到 StackPanel 中。大约花了50毫秒。我认为仍然是一个非常高的数字。
我不应该将用户控件用于这样的目的吗?这样做似乎非常面向对象,我真的看不到其他解决方案。
不过我可以看到 PropertyGrid 和 TreeView 表现良好,那么它们做了什么以及我应该做什么?
编辑:
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
using (var suspend = Dispatcher.DisableProcessing())
{
// Add all children here
for (int i = 0; i < 200; i++)
{
this.propertiesStackPanel.Children.Add(new System.Windows.Controls.Button(){Content = "Testing"});
}
}
stopwatch.Stop();
这仍然需要大约 50 毫秒。如果我更改为我自己的自定义用户控件,它会高得多。我可能会补充一点,滚动不是问题。
编辑2:
好的。它与堆栈面板无关。我发现这是因为创建 UserControls 是一项非常昂贵的操作。如果您有任何其他想法该怎么做,我很乐意听到他们:)
Edit3: 除了 InitializeComponent 方法之外,我的用户控件的构造函数中没有发生任何事情。这是我添加的用户控件的示例。
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
x:Class="PropertyBox.GroupUC"
x:Name="UserControl"
d:DesignWidth="640" d:DesignHeight="480" Background="#FF32B595" BorderThickness="0">
<Grid x:Name="LayoutRoot">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="20px"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Border x:Name="border" BorderThickness="0,1" Grid.Column="1">
<TextBox Text="TextBox" TextWrapping="Wrap" VerticalAlignment="Center" HorizontalAlignment="Right" BorderThickness="0" Padding="0" Visibility="Hidden"/>
</Border>
<Label x:Name="groupNameLabel" HorizontalAlignment="Left" Margin="5,0,0,0" VerticalAlignment="Center" Content="Label" Padding="0" Grid.Column="1"/>
<Button x:Name="expandButton" HorizontalAlignment="Left" VerticalAlignment="Center" Width="12" Height="12" Content="" Click="ExpandButtonClick" Margin="4,0,0,0" Padding="0" Grid.ColumnSpan="2" d:IsHidden="True"/>
<Image x:Name="expandButton2" Visibility="Hidden" Width="12" Height="12" HorizontalAlignment="Center" VerticalAlignment="Center" Stretch="None"/>
</Grid>
I am designing something similar a PropertyGrid where I want to show properties of objects. For special reasons I am not going to use the PropertyGrid but create my own.
For each property I have created a custom usercontrol. Now to my horror the performance is very bad. If I have something like 100 properties it takes 500 milliseconds to show them in a StackPanel/Listbox.
I did an experiment where I add 200 default UserControls to a StackPanel. It took about 50 milliseconds. Still a very high number I think.
Should I not use usercontrols for such a purpose? It seems very object-oriented to do it this way and I can not really see another solution.
However I can see that PropertyGrid and TreeView performs good, so what have they done and what should I do?
Edit:
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
using (var suspend = Dispatcher.DisableProcessing())
{
// Add all children here
for (int i = 0; i < 200; i++)
{
this.propertiesStackPanel.Children.Add(new System.Windows.Controls.Button(){Content = "Testing"});
}
}
stopwatch.Stop();
This still takes about 50 milliseconds. If I change to my own custom usercontrol it is much higher. I might add that scrolling is not a problem.
Edit2:
OK. It has nothing to do with stackpanel. I have found out that it is because creating UserControls is a very expensive operation. If you have any other idea of what to do I would gladly hear them :)
Edit3:
Nothing is going on in the constructor of my usercontrol other than InitializeComponent method. Here is an example of a usercontrol I am adding.
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
x:Class="PropertyBox.GroupUC"
x:Name="UserControl"
d:DesignWidth="640" d:DesignHeight="480" Background="#FF32B595" BorderThickness="0">
<Grid x:Name="LayoutRoot">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="20px"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Border x:Name="border" BorderThickness="0,1" Grid.Column="1">
<TextBox Text="TextBox" TextWrapping="Wrap" VerticalAlignment="Center" HorizontalAlignment="Right" BorderThickness="0" Padding="0" Visibility="Hidden"/>
</Border>
<Label x:Name="groupNameLabel" HorizontalAlignment="Left" Margin="5,0,0,0" VerticalAlignment="Center" Content="Label" Padding="0" Grid.Column="1"/>
<Button x:Name="expandButton" HorizontalAlignment="Left" VerticalAlignment="Center" Width="12" Height="12" Content="" Click="ExpandButtonClick" Margin="4,0,0,0" Padding="0" Grid.ColumnSpan="2" d:IsHidden="True"/>
<Image x:Name="expandButton2" Visibility="Hidden" Width="12" Height="12" HorizontalAlignment="Center" VerticalAlignment="Center" Stretch="None"/>
</Grid>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我怀疑您在添加数百个子项时触发了许多布局更新。
如果这是瓶颈,您可能需要考虑这样做:
这将导致调度程序在您添加控件时停止处理消息,并在最后一次性完成整个布局和渲染。
My suspicion is that you're triggering many layout updates while adding your hundreds of children.
If that is the bottleneck, you may want to consider doing:
This will cause the dispatcher to stop processing messages while you add your controls, and do the entire layout and render in one pass at the end.