使用过多自定义控件时,WPF性能下降

时间:2018-05-14 17:14:13

标签: c# wpf

我是WPF编程的新手,我的应用程序性能低下有问题。

我有一个带有网格和2列的视图。第一列是ListBox,其中是RunTypes,第二列是RunType的详细视图。在这个详细视图中,有一些属性的基本控件,如Label等,以及一个NavBar控件(来自devexpress),其中是RunConfigs。 NavBar就像一个手风琴控件,可以显示所有RunConfigs,并且可以扩展它们的细节。

我需要一个选项来控制所有字段的可见性和启用状态,因此我创建了这个自定义控件:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:controls="clr-namespace:ManualDistill.Controls"
                    xmlns:converters="clr-namespace:ManualDistill.Converters">

    <Style TargetType="{x:Type controls:EditorControl}" BasedOn="{StaticResource {x:Type ContentControl}}">
        <Style.Triggers>
            <Trigger Property="IsInEditMode" Value="True">
                <Setter Property="Content" Value="{Binding RelativeSource={RelativeSource Self}, Path=EditContent}" />
            </Trigger>
            <Trigger Property="IsInEditMode" Value="False">
                <Setter Property="Content" Value="{Binding RelativeSource={RelativeSource Self}, Path=ViewContent}" />
            </Trigger>
        </Style.Triggers>
    </Style>

</ResourceDictionary>

和cs文件:

public class EditorControl: ContentControl
{

    public EditorControl()
    {
        DefaultStyleKey = typeof(EditorControl);
        //DefaultStyleKeyProperty.OverrideMetadata(typeof(EditorControl), new FrameworkPropertyMetadata(typeof(EditorControl)));

        Messenger.Default.Register<LayoutChangeMessage>(this,
        (message) =>
        {
            if (this.Feature == message.Feature && this.DisplayField == message.DisplayField)
            {
                if (message.LayoutData != null)
                {                        
                    DispatcherHelper.RunAsync(() =>
                    {
                        ResetLayout();
                        SetLayout(message.LayoutData);
                    });
                }
            }                
        });
    }

    #region DependencyProperties
    /// <summary>
    /// If the control is currently in edit mode
    /// </summary>
    public bool IsInEditMode
    {
        get { return (bool)GetValue(IsInEditModeProperty); }
        set { SetValue(IsInEditModeProperty, value); }
    }

    public static readonly DependencyProperty IsInEditModeProperty =
        DependencyProperty.Register("IsInEditMode",
        typeof(bool),
        typeof(EditorControl),
        new PropertyMetadata(false));

    public string DisplayField
    {
        get { return (string)GetValue(DisplayFieldProperty); }
        set { SetValue(DisplayFieldProperty, value); }
    }

    public static readonly DependencyProperty DisplayFieldProperty =
       DependencyProperty.Register("DisplayField",
       typeof(string),
       typeof(EditorControl),
       new PropertyMetadata(""));

    public string Feature
    {
        get { return (string)GetValue(FeatureProperty); }
        set { SetValue(FeatureProperty, value); }
    }

    public static readonly DependencyProperty FeatureProperty =
       DependencyProperty.Register("Feature",
       typeof(string),
       typeof(EditorControl),
       new PropertyMetadata(""));

    public object EditContent
    {
        get { return GetValue(EditContentProperty); }
        set
        {                
            SetValue(EditContentProperty, value);
        }
    }

    public static readonly DependencyProperty EditContentProperty =
            DependencyProperty.Register("EditContent",
                    typeof(object),
                    typeof(EditorControl),
                    new FrameworkPropertyMetadata((object)null));

    public object ViewContent
    {
        get { return GetValue(ViewContentProperty); }
        set { SetValue(ViewContentProperty, value); }
    }

    public static readonly DependencyProperty ViewContentProperty =
            DependencyProperty.Register("ViewContent",
                    typeof(object),
                    typeof(EditorControl),
                    new FrameworkPropertyMetadata((object)null));
    #endregion

    public Visibility DefaultVisibility { get; set; }
    public bool DefaultIsEnabled { get; set; }

    protected override void OnInitialized(EventArgs e)
    {
        base.OnInitialized(e);

        this.DefaultVisibility = this.Visibility;
        this.DefaultIsEnabled = this.IsEnabled;

        if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
        {
            var layouts = StillLayoutManager.Instance.GetActiveLayouts(this.Feature, this.DisplayField);
            if (layouts != null)
            {
                SetLayout(layouts);
            }
        }
    }

    private void ResetLayout()
    {
        this.Visibility = this.DefaultVisibility;
        this.IsEnabled = this.DefaultIsEnabled;
    }

    private void SetLayout(IEnumerable<ILayoutData> layouts)
    {
        foreach (var data in layouts)
        {
            this.Visibility = data.Visibility ? Visibility.Visible : Visibility.Collapsed;
            this.IsEnabled = data.Editable;                
        }            
    }
}

因此,您可以看到我有2个内容属性用于编辑/查看,并根据编辑模式在Content属性上设置它们。这个自定义控件就像每个字段或我需要的每个对象的容器一样工作。然后可以通过操纵这些控件的系统来管理字段或对象可见性(这与问题无关,这只是为什么我在那里有这些控件的解释)

问题是,当我点击带有RunType的列表框时,我必须等待几秒钟才能看到详细信息。创建所有控件然后处理,以便不记住任何内容。 RunType集合将被更改,因此可以添加更多或删除。所以控件总是动态创建的。

RunManagement,RunTypeControl和RunConfigControl的xaml文件位于:

<UserControl ...> 
      <Grid cmn:GridUtils.ColumnDefinitions="Auto, *">
                <lc:LayoutGroup Orientation="Vertical" View="GroupBox" Header="{lex:LocTextUpper RunType}" MinWidth="250">
                    <dxe:ListBoxEdit x:Name="RunTypesList" 
                                     ItemsSource="{Binding RunTypes}"
                                     SelectedItem="{Binding SelectedRunType}">
                        <dxe:ListBoxEdit.ContextMenu>
                            <ContextMenu>
                                <MenuItem Header="{lex:LocText Copy}" Command="{Binding CmdCopy}"/>
                            </ContextMenu>
                        </dxe:ListBoxEdit.ContextMenu>
                        <dxe:ListBoxEdit.ItemTemplate>
                            <DataTemplate>
                                <TextBlock HorizontalAlignment="Left" Text="{Binding Label}" TextWrapping="Wrap"/>
                            </DataTemplate>
                        </dxe:ListBoxEdit.ItemTemplate>
                    </dxe:ListBoxEdit>
                </lc:LayoutGroup>

                <Grid Grid.Column="1">                        
                    <local:RunTypeControl DataContext="{Binding SelectedRunType}" />
                </Grid>
        </Grid>
</UserControl>

...

<UserControl ...>        
    <Grid  Margin="10">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>

        <lc:LayoutControl Orientation="Horizontal">
            <lc:LayoutGroup Orientation="Vertical">  
                <controls:EditorControl IsInEditMode="{Binding IsInEditMode}">
                    <controls:EditorControl.ViewContent>
                        <lc:LayoutItem Label="{lex:LocText Label}">
                            <TextBlock Text="{Binding Label}" />
                        </lc:LayoutItem>
                    </controls:EditorControl.ViewContent>
                    <controls:EditorControl.EditContent>
                        <lc:LayoutItem Label="{lex:LocText Label}">
                            <dxe:TextEdit Text="{Binding Label}"/>
                        </lc:LayoutItem>
                    </controls:EditorControl.EditContent>
                </controls:EditorControl>                        
            </lc:LayoutGroup>
            <lc:LayoutGroup Orientation="Vertical">
                <lc:LayoutItem>
                </lc:LayoutItem>
            </lc:LayoutGroup>
        </lc:LayoutControl>

        <lc:LayoutGroup Grid.Row="1" Margin="0,5" View="Tabs">
            <lc:LayoutGroup Orientation="Horizontal" Header="{Binding TabHeaderRunStates}">
                <lc:LayoutItem>
                        <dxn:NavBarControl Name="navBar" Grid.Column="1" ItemsSource="{Binding RunConfigs}" Margin="10 0 0 0">
                            <dxn:NavBarControl.ItemTemplate>
                                <DataTemplate>
                                    <controls:EditorControl IsInEditMode="{Binding IsInEditMode}">
                                        <controls:EditorControl.ViewContent>
                                            <dxn:NavBarGroup DisplaySource="Content" Header="{Binding DisplayName}" IsExpanded="False">
                                                <dxn:NavBarGroup.Content>
                                                    <local:RunConfigControl DataContext="{Binding}"/>                                                       
                                                </dxn:NavBarGroup.Content>
                                            </dxn:NavBarGroup>
                                        </controls:EditorControl.ViewContent>
                                        <controls:EditorControl.EditContent>
                                            <dxn:NavBarGroup DisplaySource="Content" Header="{Binding DisplayName}" IsExpanded="False">
                                                <dxn:NavBarGroup.Content>
                                                    <local:RunConfigControl DataContext="{Binding}"/>
                                                </dxn:NavBarGroup.Content>
                                            </dxn:NavBarGroup>
                                        </controls:EditorControl.EditContent>
                                    </controls:EditorControl>
                                </DataTemplate>
                            </dxn:NavBarControl.ItemTemplate>
                            <dxn:NavBarControl.View>
                                <dxn:ExplorerBarView Click="ExplorerBarView_Click"
                                                     GroupAdding="ExplorerBarView_GroupAdding" />
                            </dxn:NavBarControl.View>
                        </dxn:NavBarControl> 
                </lc:LayoutItem>
            </lc:LayoutGroup>
        </lc:LayoutGroup>
    </Grid>
</UserControl>

...

<UserControl ...>
    <AdornerDecorator>
        <lc:LayoutControl Orientation="Horizontal">
            <lc:LayoutGroup Orientation="Vertical"> 

                <controls:EditorControl IsInEditMode="{Binding IsInEditMode}">
                    <controls:EditorControl.ViewContent>
                        <lc:LayoutItem Label="{lex:LocText CloseCut}">
                            <TextBlock Text="{Binding CloseCut}" />
                        </lc:LayoutItem>
                    </controls:EditorControl.ViewContent>
                    <controls:EditorControl.EditContent>
                        <lc:LayoutItem Label="{lex:LocText CloseCut}" HorizontalContentAlignment="Right">
                            <dxe:TextEdit Text="{Binding CloseCut}" Width="200"/>
                        </lc:LayoutItem>
                    </controls:EditorControl.EditContent>
                </controls:EditorControl>

                <controls:EditorControl IsInEditMode="{Binding IsInEditMode}">
                    <controls:EditorControl.ViewContent>
                        <lc:LayoutItem Label="{lex:LocText PressureSetpoint}">
                            <TextBlock Text="{Binding PressureSetpoint}"/>
                        </lc:LayoutItem>
                    </controls:EditorControl.ViewContent>
                    <controls:EditorControl.EditContent>
                        <lc:LayoutItem Label="{lex:LocText PressureSetpoint}" HorizontalContentAlignment="Right">
                            <dxe:TextEdit Text="{Binding PressureSetpoint}" Width="200"/>
                        </lc:LayoutItem>
                    </controls:EditorControl.EditContent>
                </controls:EditorControl>

                <controls:EditorControl IsInEditMode="{Binding IsInEditMode}">
                    <controls:EditorControl.ViewContent>
                        <lc:LayoutItem Label="{lex:LocText SpinningBand}">
                            <TextBlock Text="{Binding SpinningBand, Converter={StaticResource BooleanOnOffConverter}}" Width="200" />
                        </lc:LayoutItem>
                    </controls:EditorControl.ViewContent>
                    <controls:EditorControl.EditContent>
                        <lc:LayoutItem Label="{lex:LocText SpinningBand}" HorizontalContentAlignment="Right">
                            <dxe:ToggleSwitchEdit IsChecked="{Binding SpinningBand}" IsEnabled="True" ContentPlacement="Both"
                                                  CheckedStateContent="{lex:LocText ON}" 
                                                  UncheckedStateContent="{lex:LocText OFF}"/>
                        </lc:LayoutItem>
                    </controls:EditorControl.EditContent>
                </controls:EditorControl>

                ...

            </lc:LayoutGroup>
        </lc:LayoutControl>
    </AdornerDecorator>
</UserControl>

我知道有太多的xaml代码我试图缩短它并显示重要的部分。

如果我不使用自定义控件并只有2个列表框来选择runtype和runco​​nfigs并查看详细信息,那么它的工作速度很快。

我计算了在此过程中创建的控件数量。如果一个runtype有4个属性和5个runco​​nfigs,每个runco​​nfig有8个属性,那么我创建了88个自定义控件:

8个自定义控件=&gt; 4个属性(4个用于编辑模式,4个用于视图模式) 80个自定义控件=&gt; 5 x 8 runco​​nfig属性(40个控件用于编辑模式,40个用于视图模式)

这是导致性能降低的问题,但我认为WPF可以在没有问题的情况下实现这一目标,即使是更多的控件。我做错了什么,我是否需要在控件上设置一些设置?

感谢您的帮助。

0 个答案:

没有答案