簡體   English   中英

WPF - UserControls非常慢

[英]WPF - UserControls very slow

我正在設計類似於PropertyGrid的東西,我想在其中顯示對象的屬性。 出於特殊原因,我不打算使用PropertyGrid,而是創建自己的。

對於每個屬性,我創建了一個自定義用戶控件。 令我恐懼的是,表現非常糟糕。 如果我有100個屬性,則需要500毫秒才能在StackPanel / Listbox中顯示它們。

我做了一個實驗,在那里我向StackPanel添加了200個默認UserControl。 花了大約50毫秒。 我認為仍然是一個非常高的數字。

我不應該將usercontrol用於此目的嗎? 這樣做似乎非常面向對象,我無法真正看到另一種解決方案。

但是我可以看到PropertyGrid和TreeView表現良好,所以他們做了什么,我該怎么辦?

編輯:

        Stopwatch stopwatch = new Stopwatch();
        stopwatch.Start();
        using (var suspend = Dispatcher.DisableProcessing())
        {
            // Add all children here
            for (int i = 0; i < 200; i++)
            {
                this.propertiesStackPanel.Children.Add(new System.Windows.Controls.Button(){Content = "Testing"});
            }
        }
        stopwatch.Stop();

這仍然需要大約50毫秒。 如果我更改為我自己的自定義用戶控件,它會更高。 我可能會補充說滾動不是問題。

EDIT2:

好。 它與stackpanel無關。 我發現這是因為創建UserControls是一項非常昂貴的操作。 如果您對我該做什么有任何其他想法,我很樂意聽到他們:)

Edit3:除了InitializeComponent方法之外,我的usercontrol的構造函數中沒有任何內容。 這是我正在添加的用戶控件的示例。

<UserControl
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"
mc:Ignorable="d"
x:Class="PropertyBox.GroupUC"
x:Name="UserControl"
d:DesignWidth="640" d:DesignHeight="480" Background="#FF32B595" BorderThickness="0">

<Grid x:Name="LayoutRoot">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="20px"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
    <Border x:Name="border" BorderThickness="0,1" Grid.Column="1">
        <TextBox Text="TextBox" TextWrapping="Wrap" VerticalAlignment="Center" HorizontalAlignment="Right" BorderThickness="0" Padding="0" Visibility="Hidden"/>
    </Border>
    <Label x:Name="groupNameLabel" HorizontalAlignment="Left" Margin="5,0,0,0" VerticalAlignment="Center" Content="Label" Padding="0" Grid.Column="1"/>
    <Button x:Name="expandButton" HorizontalAlignment="Left" VerticalAlignment="Center" Width="12" Height="12" Content="" Click="ExpandButtonClick" Margin="4,0,0,0" Padding="0" Grid.ColumnSpan="2" d:IsHidden="True"/>
    <Image x:Name="expandButton2" Visibility="Hidden"  Width="12" Height="12" HorizontalAlignment="Center" VerticalAlignment="Center" Stretch="None"/>
</Grid>

我懷疑你在添加數百個孩子的同時觸發了許多布局更新。

如果這是瓶頸,您可能需要考慮:

using(var suspend = Dispatcher.DisableProcessing())
{
       // Add all children here
}

這將導致調度程序在您添加控件時停止處理消息,並執行整個布局並在最后一次渲染中進行渲染。

如果您發現創建許多UserControl的操作過於昂貴,那么您根本不需要創建UserControl的解決方案呢?

我不是WPF專家,但是如何使用ListBoxListView數據綁定對象列表,每個對象代表被檢查對象的一個​​屬性? 而不是StackPanel ,你有一個ListboxListView ; 而不是UserControl ,您將定義一個或多個DataTemplate


基本代碼示例:

定義表示自定義屬性網格中的條目的類型; 例如:

public namespace YourApplication
{
    public class Prop
    {
        public string Name { get; set; }
        public Type Type { get; set; }
    }

    public class Props : List<Prop> { }
}

然后,在XAML中(我目前無法檢查這個100%的正確性,但希望你能得到這個想法):

<Window ... xmlns:local="clr-namespace:YourApplication">
  <Window.Resources>
    <!-- this Prop list serves only as a demonstration in the XAML designer -->
    <local:Props x:Key="somePropsForDemonstration">
      <local:Prop Name="Name" Type="System.String" />
      <local:Prop Name="Age" Type="System.TimeSpan" />
    </local:Props>
  </Window.Resources>
  <!-- here's your StackPanel replacement; bind it to a real items source -->
  <ListView ItemsSource={StaticResource somePropsForDemonstration}>
    <ListView.ItemsTemplate>
      <!-- this is a UI template that defines how a Prop object gets displayed;
           together with the Prop type, it replaces your UserControl -->
      <DataTemplate DataType="local:Prop">
        <StackPanel Orientation="Horizontal">
          <TextBlock Text="{Binding Name}" FontWeight="Bold" />
          <TextBlock Text=": " />
          <TextBlock Text="{Binding Type}" FontStyle="Italic" />
        </StackPanel>
      </DataTemplate>
    </ListView.ItemsTemplate>
  </ListView>
</Window>

我似乎記得有多種方法可以獲得“多態”數據模板; 即。 根據屬性的類型,您可以使用不同的Prop子類,並且每個項類型也使用不同的數據模板。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM