性能UserControl/CustomControl

mic*_*cro 8 wpf performance user-controls custom-controls datatemplate

我已经在网上搜索了很多网站,但没有找到任何解决方案.声明是,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));
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

用户控件:

<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>
Run Code Online (Sandbox Code Playgroud)

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)));
    }
}
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

主窗口:

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());
    }
}
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

使用CustomControl时,MessageBox显示约.20秒在我的电脑上,但使用UserControl时,大约需要一分钟!将Grid与Grid一起替换为TextBox,它甚至比CustomControl(~16秒)更快.

有人能看出瓶颈在哪里吗?这个问题在我的实际应用程序中引发,模板/控件会更加复杂.

非常感谢,

CJK*_*CJK 1

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

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

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