Wrappanel在切换itemssource时性能下降

时间:2014-02-03 04:11:20

标签: c# wpf performance localization wrappanel

目前我正在开发一个项目,该项目在wrappanel中显示项目列表,每个项目的模板将包含一个TextBlock,它显示项目的名称和带有本地化的文本标签(using resx approach)。

问题是,list_1是1000个项目,第一次加载需要大约1秒,这很好,但是,当我切换warppanel加载list_2(1000项长)并多次返回list_1 ,现在需要2秒钟加载,再过30到40次切换,加载列表需要5秒钟,每次切换ItemsSource时,加载列表需要更长的时间。

但是对于相同的逻辑,没有本地化,加载列表的加载时间是恒定的(大约1秒),无论你切换ItemsSource多少次。

以前有人有这个问题吗?请帮忙..谢谢!

Sample project download link

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

0 个答案:

没有答案