『QQ:1353814576』

WPF 列表ListView控件一次性加载大量数据性能优化

WPF 列表ListView控件一次性加载大量数据性能优化

WPF


WPF列表控件一次性加载大量数据是性能的优化处理

之前自己写了一款分析IIS本地日志信息的软件,中间碰到一个问题就是某些日志的行数基本都是以千为单位甚至以万为单位,当时测试是软件数据日志数据加载时间非常长界面才能响应过来,当时想了两个解决方案

  1. 采用分页方式 进行展示 每次只加载指定数量的
  2. wpf UI Virtualization

理论上来说其实方案1 是比较合适的办法 个人比较推荐

但由于软件上的一些功能设计问题 最终采用了方案二,实测加载近10w数据加载完成显示时间差不多需要10秒左右 基本满足了我的需求

Wpf UI Virtualization 设置的核心属性

下面上代码

  <ListView Margin="5" Style="{x:Null}" 
                              ItemsSource="{Binding Path=LogAnalysisViewData.DataSource,IsAsync=True}"  
                              VirtualizingPanel.IsVirtualizing="True" 
                              VirtualizingPanel.VirtualizationMode="Recycling" 
                              VirtualizingPanel.IsContainerVirtualizable="True" 
                              VirtualizingPanel.IsVirtualizingWhenGrouping="True">

                        <ListView.InputBindings>
                            <KeyBinding Key="Space" Command="{Binding MenuBarSwitchChanged}" />
                        </ListView.InputBindings>

                        <ListView.View>
                            <GridView>

                                <GridView.Columns>
                                    <GridViewColumn  Header="服务器类型/ip" Width="180">
                                        <GridViewColumn.CellTemplate>
                                            <DataTemplate>
                                                <VirtualizingStackPanel Orientation="Horizontal">
                                                    <TextBlock  Text="{Binding Path=WebServer}" />
                                                    <TextBlock Text="/"/>
                                                    <TextBlock Text="{Binding Path=IpAddress}" />
                                                </VirtualizingStackPanel>
                                            </DataTemplate>
                                        </GridViewColumn.CellTemplate>
                                    </GridViewColumn>

                                    <GridViewColumn DisplayMemberBinding="{Binding Date}" mah:VisibilityHelper.IsVisible="False" Header="日期"/>
                                    <GridViewColumn DisplayMemberBinding="{Binding Time}" Header="时间"/>
                                    <GridViewColumn DisplayMemberBinding="{Binding Method}" Header="请求方式" />
                                    <GridViewColumn  Header="访问端口" DisplayMemberBinding="{Binding RequestPort}" />

                                    <GridViewColumn  Header="访客标识" Width="100">
                                        <GridViewColumn.CellTemplate>
                                            <DataTemplate>
                                                <VirtualizingStackPanel Orientation="Horizontal" HorizontalAlignment="Left" Cursor="Hand" ToolTip="{Binding Path=UserAgent}">

                                                    <TextBlock Text="--" mah:VisibilityHelper.IsCollapsed="{Binding Path=IsSpider}" />

                                                    <Button ToolTip="点击nslookup命令验证蜘蛛真伪" Padding="5 0 5 0" Cursor="Hand" Command="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=mah:MetroWindow},Path=DataContext.SpiderPoolNslookupClick}" CommandParameter="{Binding Path=ClientIpAddress}"  mah:VisibilityHelper.IsVisible="{Binding Path=IsSpider}">
                                                        <VirtualizingStackPanel Orientation="Horizontal">
                                                            <iconPacks:PackIconMaterial ToolTip="点击验证蜘蛛"  Height="12" Foreground="DarkMagenta" Kind="Spider" VerticalAlignment="Center" mah:VisibilityHelper.IsVisible="{Binding Path=IsSpider}"></iconPacks:PackIconMaterial>
                                                            <TextBlock ToolTip="{Binding Path=UserAgent}"  Text="{Binding Spider.Name}" Foreground="DarkMagenta" FontSize="12" ></TextBlock>
                                                        </VirtualizingStackPanel>
                                                    </Button>
                                                </VirtualizingStackPanel>
                                            </DataTemplate>
                                        </GridViewColumn.CellTemplate>
                                    </GridViewColumn>

                                    <GridViewColumn  Header="访客ip" Width="100">
                                        <GridViewColumn.CellTemplate>
                                            <DataTemplate>
                                                <TextBox VerticalContentAlignment="Center" IsReadOnly="True" HorizontalAlignment="Stretch" Margin="0 3 0 3" BorderThickness="0" Foreground="Blue" Text="{Binding ClientIpAddress}"  mah:TextBoxHelper.SelectAllOnFocus="True" />
                                            </DataTemplate>
                                        </GridViewColumn.CellTemplate>
                                    </GridViewColumn>

                                    <GridViewColumn  Header="访问地址" Width="200">
                                        <GridViewColumn.CellTemplate>
                                            <DataTemplate>
                                                <TextBox VerticalContentAlignment="Center" IsReadOnly="True" HorizontalAlignment="Stretch" BorderBrush="Transparent" BorderThickness="0" Foreground="Blue" Text="{Binding Url}"  mah:TextBoxHelper.SelectAllOnFocus="True" />
                                            </DataTemplate>
                                        </GridViewColumn.CellTemplate>
                                    </GridViewColumn>

 
           

                                </GridView.Columns>
                            </GridView>
                        </ListView.View>
                    </ListView>