性能 UserControl / CustomControl

9

我已经在网络上搜索了很多网站,但没有找到任何解决方案。有人说,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 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>

自定义控件:

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>

主窗口:

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());
    }
}

主窗口内容:

<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时,需要大约一分钟的时间!将控件替换为其Grid和TextBox后,甚至比CustomControl更快(约16秒)。
有人能看到瓶颈在哪里吗?这个问题出现在我的实际应用程序中,其中模板/控件会更加复杂。
非常感谢,
微小
1个回答

1
这是一个晚回答,但基本区别在于用户控件几乎像窗口一样,您有控件本身,然后可以添加其他控件,如按钮、网格、文本框等。窗口和用户控件之间的基本区别在于用户控件可以并且必须在窗口中显示。
另一方面,自定义控件只是一个控件,它可以用于创建具有特定功能的控件,其中没有内置控件,或者为现有控件(如按钮、文本框等)提供特定的样式,以匹配应用程序的主题。您还可以通过使用自定义控件向现有控件添加额外功能,例如向文本框添加标签以显示其目的。
加载时间的差异实质上反映了用户和自定义控件不同目的的区别,对于用户控件,它会加载控件及其内部元素,因此加载时间可能较长。对于自定义控件,只需加载控件本身,因此它的加载时间不会比大多数内置WPF控件更长,即按钮自定义控件不应该比内置按钮控件需要更长的时间。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接