C# Wrappanel在切换itemssource时性能低下

C# Wrappanel在切换itemssource时性能低下,c#,wpf,performance,localization,wrappanel,C#,Wpf,Performance,Localization,Wrappanel,目前,我正在从事一个项目,该项目在wrappanel中显示一个项目列表,每个项目的模板将包含一个TextBlock,其中显示项目名称和带有本地化的文本标签 问题是,列表_1有1000个项目长,第一次加载大约需要1秒,这很好,但是,当我切换warppanel以加载列表_2 1000个项目并多次返回列表_1时,现在加载需要2秒,再切换30-40次后,加载列表需要5秒,每次切换ItemsSource时,加载列表所需的时间都会更长 但是对于相同的逻辑,在没有本地化的情况下,加载列表的时间是恒定的,大约1

目前,我正在从事一个项目,该项目在wrappanel中显示一个项目列表,每个项目的模板将包含一个TextBlock,其中显示项目名称和带有本地化的文本标签

问题是,列表_1有1000个项目长,第一次加载大约需要1秒,这很好,但是,当我切换warppanel以加载列表_2 1000个项目并多次返回列表_1时,现在加载需要2秒,再切换30-40次后,加载列表需要5秒,每次切换ItemsSource时,加载列表所需的时间都会更长

但是对于相同的逻辑,在没有本地化的情况下,加载列表的时间是恒定的,大约1秒,无论您切换ItemsSource多少次

以前有人有过这个问题吗?请帮忙。。谢谢


WrapPanel不支持UI虚拟化,这就是您最终等待的原因。为什么在ListView中还需要WrapPanel?@devhedgehog奇怪的是,每次使用新列表更新itemsSource后,等待时间都在增加,在itemsSource更新100次后,等待时间从1秒增加到10秒。由于我需要从左到右逐行显示项目,所以请使用WrapPanel作为ListView的ItemsPanel
<Window
    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="LocalizeProject.MainWindow"
    Title="MainWindow" Height="600" Width="800">
<Window.Resources>
    <Style x:Key="ListViewItemStyle1" TargetType="{x:Type ListViewItem}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ListViewItem}">
                    <Grid Height="70" HorizontalAlignment="Left" Width="70">
                        <TextBlock HorizontalAlignment="Stretch" Margin="0,0,0,34" TextWrapping="Wrap" Text="{Binding ItemName}" Width="Auto" Height="Auto"/>
                        <Grid Height="34" VerticalAlignment="Bottom" Background="Red" Margin="4">
                            <TextBlock x:Name="SoldoutLabel" Margin="0,8" TextWrapping="Wrap" Text="{Resx ResxName=LocalizeProject.MultiLanguage, Key=WPF_沽清}" Foreground="White"/>
                        </Grid>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <ItemsPanelTemplate x:Key="ItemsPanelTemplate1">
        <WrapPanel Orientation="Horizontal"
        Width="{Binding ActualWidth,RelativeSource={RelativeSource AncestorType=ScrollContentPresenter}}" ScrollViewer.VerticalScrollBarVisibility="Disabled" d:DesignWidth="386" />
    </ItemsPanelTemplate>
    <Style x:Key="ListViewItemStyle2" TargetType="{x:Type ListViewItem}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ListViewItem}">
                    <Grid Height="70" HorizontalAlignment="Left" Width="70">
                        <TextBlock HorizontalAlignment="Stretch" Margin="0,0,0,34" TextWrapping="Wrap" Text="{Binding ItemName}" Width="Auto" Height="Auto"/>
                        <Grid Height="34" VerticalAlignment="Bottom" Background="Red" Margin="4">
                            <TextBlock Margin="0,8" TextWrapping="Wrap" Text="{Binding DataContext.TestString, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListView}}}" Foreground="White"/>
                        </Grid>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <ItemsPanelTemplate x:Key="ItemsPanelTemplate2">
        <VirtualizingStackPanel IsItemsHost="True"/>
    </ItemsPanelTemplate>

</Window.Resources>
<Grid d:DataContext="{d:DesignData /SampleData/MainWindowViewModelSampleData.xaml}">
    <ListView x:Name="ListViewLeft" HorizontalAlignment="Left" Margin="8,50,0,80" ItemsSource="{Binding AllItemMasterList}" Width="275" ItemContainerStyle="{DynamicResource ListViewItemStyle1}" ItemsPanel="{DynamicResource ItemsPanelTemplate1}" LayoutUpdated="ListViewLeft_LayoutUpdated">
        <ListView.View>
            <GridView>
                <GridViewColumn/>
            </GridView>
        </ListView.View>
    </ListView>
    <ListView x:Name="ListViewRight" Margin="287,50,228,80" ItemsSource="{Binding AllItemMasterList_1}" ItemContainerStyle="{DynamicResource ListViewItemStyle2}" ItemsPanel="{DynamicResource ItemsPanelTemplate1}">
        <ListView.View>
            <GridView>
                <GridViewColumn/>
            </GridView>
        </ListView.View>
    </ListView>
    <Button x:Name="LeftPageUpButton" Content="List 1"  HorizontalAlignment="Left" Height="40" Margin="8,0,0,36" VerticalAlignment="Bottom" Width="105" Click="LeftPageUpButton_Click"/>
    <Button x:Name="LeftPageDownButton" Content="List 2"  HorizontalAlignment="Left" Height="40" Margin="117,0,0,36" VerticalAlignment="Bottom" Width="105" Click="LeftPageDownButton_Click"/>
    <Button x:Name="RightPageUpButton" Content="List 1"  Height="40" Margin="287,0,0,36" VerticalAlignment="Bottom" Click="RightPageUpButton_Click" HorizontalAlignment="Left" Width="105"/>
    <Button x:Name="RightPageDownButton" Content="List 2" Height="40" Margin="0,0,283,36" VerticalAlignment="Bottom" Click="RightPageDownButton_Click" HorizontalAlignment="Right" Width="105"/>
    <TextBlock HorizontalAlignment="Left" Height="38" Margin="8,8,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="214"><Run Language="zh-tw" Text="With Localization"/></TextBlock>
    <TextBlock Height="38" Margin="287,8,283,0" TextWrapping="Wrap" VerticalAlignment="Top"><Run Language="zh-tw" Text="Without Localization"/></TextBlock>
    <TextBox Height="23" HorizontalAlignment="Left" Margin="556,458,0,0" Name="textBox1" VerticalAlignment="Top" Width="222" Text="{Resx ResxName=LocalizeProject.MultiLanguage, Key=WPF_請稍候}"/>
</Grid>