
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(); @H_419_7@这仍然需要大约50毫秒.如果我更改为我自己的自定义用户控件,它会更高.我可能会补充说滚动不是问题. @H_419_7@EDIT2: @H_419_7@好.它与stackpanel无关.我发现这是因为创建UserControls是一项非常昂贵的 *** 作.如果您对我该做什么有任何其他想法,我很乐意听到他们:) @H_419_7@EDIT3:除了InitializeComponent方法之外,我的usercontrol的构造函数中没有任何内容.这是我正在添加的用户控件的示例. @H_419_7@
<UserControlxmlns="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" 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" 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>解决方法 我怀疑你在添加数百个孩子的同时触发了许多布局更新. @H_419_7@ @H_419_7@如果这是瓶颈,您可能需要考虑: @H_419_7@
using(var suspend = dispatcher.disableProcessing()){ // Add all children here} @H_419_7@这将导致调度程序在您添加控件时停止处理消息,并执行整个布局并在最后一次渲染中进行渲染. 总结 以上是内存溢出为你收集整理的c# – WPF – UserControls非常慢全部内容,希望文章能够帮你解决c# – WPF – UserControls非常慢所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)