我已经在网上搜索了很多网站,但没有找到任何解决方案。声明是,UserControl和CustomControl之间没有性能差异。
但我有以下测试类X、UserControl、CustomControl和MainWindow:
public class X : INotifyPropertyChanged
{
private string _title;
public string Title
{
get
{
return _title;
}
set
{
if (value == _title)
{
return;
}
_title = value;
OnPropertyChanged("Title");
}
}
public event PropertyChangedEventHandler PropertyChanged;
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}UserControl:
<UserControl x:Class="controlperformance.DisplayView"
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">
<Grid Name="root" Background="LightGray">
<TextBlock Text="{Binding Title}" />
</Grid>
</UserControl>CustomControl:
public class DisplayControl : Control
{
#region Title
public string Title
{
get
{
return (string)GetValue(TitleProperty);
}
set
{
SetValue(TitleProperty, value);
}
}
public static readonly DependencyProperty TitleProperty = DependencyProperty.Register("Title",
typeof(string),
typeof(DisplayControl),
new PropertyMetadata(default(string)));
#endregion
static DisplayControl()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(DisplayControl), new FrameworkPropertyMetadata(typeof(DisplayControl)));
}
}Xaml:
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:DisplayControl}">
<Grid Background="white">
<TextBlock Text="{TemplateBinding Title}" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>MainWindow:
public partial class MainWindow : Window
{
Stopwatch sw = new Stopwatch();
public MainWindow()
{
InitializeComponent();
Loaded += OnLoaded;
sw.Start();
ObservableCollection<X> list = new ObservableCollection<X>();
Random r = new Random();
for (int i = 0; i < 50000; i++)
{
list.Add(new X { Title = r.Next().ToString()});
}
itemscontrol.ItemsSource = list;
}
private void OnLoaded(object sender, RoutedEventArgs routedEventArgs)
{
sw.Stop();
MessageBox.Show(sw.Elapsed.ToString());
}
}MainWindow内容:
<ItemsControl Name="itemscontrol">
<ItemsControl.ItemTemplate>
<!--<DataTemplate DataType="{x:Type Controlperformance:X}">
<Controlperformance:DisplayView DataContext="{Binding}" />
</DataTemplate>-->
<DataTemplate DataType="{x:Type Controlperformance:X}">
<Controlperformance:DisplayControl Title="{Binding Title}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>使用CustomControl时,MessageBox显示约。在我的电脑上花了20秒,但当使用UserControl时,大约需要一分钟!将控件替换为网格TextBox,它甚至比CustomControl更快(~16秒)。
有人能看到瓶颈在哪里吗?这个问题出现在我的实际应用程序中,其中的模板/控件会复杂得多。
非常感谢,
微
发布于 2015-05-05 11:26:18
这是一个迟来的答案,但基本的区别是,用户控件几乎就像一个窗口,因为您拥有控件本身,然后可以向其添加其他控件,如按钮、网格、文本框等。窗口和用户控件之间的基本区别在于,用户控件可以而且必须显示在窗口中。
另一方面,自定义控件只是一个控件,它可以用来创建一个具有特定功能的控件,该控件没有内置控件,或者给现有的控件一个特定的样式,如按钮,文本框等,以匹配您的应用程序的主题。您还可以通过使用自定义控件向现有控件添加额外的功能,例如向文本框添加标签以显示其用途。
加载时间的差异本质上反映了用户控件和自定义控件的不同用途,对于用户控件,它加载控件和该控件中的元素,因此加载时间可能会更长。使用自定义控件,只有控件本身必须加载,所以加载它不会比大多数内置WPF控件花费更长的时间,即按钮自定义控件不应该比内置按钮控件花费更长的时间。
https://stackoverflow.com/questions/16847772
复制相似问题