0

我将一个包含 500 个奇数缺口的列表绑定到一个包含每个状态图像的列表。列表的滚动非常缓慢,在具有不同列表的选项卡之间滑动也是如此。

这都是由我最近添加这些图像的更改引起的。

有没有办法加快速度?

我的位图(非常小的 16*16):

<BitmapImage x:Key="ActiveIcon" UriSource="/WPFClient;component/Images/active.png" />
<BitmapImage x:Key="IdleIcon" UriSource="/WPFClient;component/Images/idle.png" />
<BitmapImage x:Key="AwayIcon" UriSource="/WPFClient;component/Images/away.png" />
<BitmapImage x:Key="UnknownIcon" UriSource="/WPFClient;component/Images/unknown.png" />

我的列表 :

<ListBox ItemsSource="{Binding Users}">
   <ListBox.ItemTemplate>
     <DataTemplate>
       <DockPanel>
         <Image Source="{Binding Status, Converter={StaticResource UserStatusToIconConverter}}" Height="16" Width="16" Margin="0,0,5,0" />
         <TextBlock Text="{Binding Nick}" />
       </DockPanel>
     </DataTemplate>
   </ListBox.ItemTemplate>
</ListBox>

我的转换器:

public class UserStatusToIconConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        string userStatus = value.ToString();
        string iconName = "UnknownIcon";

        switch (userStatus)
        {
            case "Active":
                iconName = "ActiveIcon";
                break;
            case "Idle":
                iconName = "IdleIcon";
                break;
            case "Away":
                iconName = "AwayIcon";
                break;
        }

        return iconName;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

注意:代码也不能像现在这样工作,实际上没有图像显示。但是我认为那是某个地方的一个小细节。

4

2 回答 2

1

尝试使用 VirtualizationStackPanel 会有一些改进

<ListBox VirtualizingStackPanel.IsVirtualizing="True"
                 VirtualizingStackPanel.VirtualizationMode="Recycling">

</ListBox>

在框架 4.5 VirtualizingPanel 可用并且设置 VirtualizingPanel.ScrollUnit="Item" 将提供良好的性能改进

于 2013-08-20T17:35:23.630 回答
0

解决这个问题的最简单方法是将转换器更改为:

public class UserStatusToIconConverter : IValueConverter
{
    private static readonly Uri ActiveIcon = new Uri("pack://application:,,,/WPFClient;component/Images/active.png");
    private static readonly Uri IdleIcon = new Uri("pack://application:,,,/WPFClient;component/Images/idle.png");
    private static readonly Uri AwayIcon = new Uri("pack://application:,,,/WPFClient;component/Images/away.png");
    private static readonly Uri UnknownIcon = new Uri("pack://application:,,,/WPFClient;component/Images/unknown.png");

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var userStatus = value.ToString();
        switch (userStatus)
        {
            case "Active":
                return ActiveIcon;
            case "Idle":
                return IdleIcon;
            case "Away":
                return AwayIcon;
            default:
                return UnknownIcon;
        }
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

减速是由于绑定不当造成的。

于 2013-08-20T17:42:12.733 回答