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

xml:

 <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 秒)。

有人能看出瓶颈在哪里吗?这个问题出现在我的实际应用程序中,其中模板/控件会复杂得多。

非常感谢,

4

1 回答 1

1

这是一个较晚的答案,但基本区别在于用户控件几乎就像一个窗口,因为您拥有控件本身,然后可以将其他控件(如按钮、网格、文本框等)添加到其中。窗口和用户控件之间的基本区别在于,用户控件可以而且必须显示在窗口中。

另一方面,自定义控件只是一个控件,它可用于创建具有特定功能的控件,但没有内置控件或为现有控件(如按钮、文本框等)提供特定样式以匹配您的应用程序的主题。您还可以通过使用自定义控件向现有控件添加额外功能,例如向文本框添加标签以显示其用途。

加载时间的差异本质上反映了用户控件和自定义控件的不同用途,用户控件会加载控件和该控件中的元素,因此加载时间可能会更长。使用自定义控件,只有控件本身必须加载,因此加载时间不会比大多数内置 WPF 控件长,即按钮自定义控件不应该比内置按钮控件花费更长的时间。

于 2015-05-05T03:26:17.830 回答