性能UserControl / CustomControl

时间:2013-05-30 23:59:45

标签: 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));
        }
    }
}

用户控件:

<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>

主窗口:

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时,大约需要一分钟!将Control与Grid一起替换为TextBox,它甚至比CustomControl(~16秒)更快。

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

非常感谢,

1 个答案:

答案 0 :(得分:-1)

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

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

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

相关问题