1

有一个带有以下 ItemContainerStyle 的列表框,它绑定到 ViewModel 类,但是当滚动列表框时,它的性能并不流畅。我在网上查了一下,我用列表框中的普通 stackpanel 替换了 visualstackpanel。是的,它滚动流畅,但在加载数据时会冻结。有什么我可以做的吗?

    <Style TargetType="ListBoxItem" x:Key="ListBoxItem">
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="BorderThickness" Value="0" />
        <Setter Property="BorderBrush" Value="Transparent" />
        <Setter Property="Padding" Value="0" />
        <Setter Property="HorizontalContentAlignment" Value="Left"/>
        <Setter Property="VerticalContentAlignment" Value="Top"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ListBoxItem">
                    <Border x:Name="LayoutBorder" BorderThickness="0.3,0.3,0.3,0.3" BorderBrush="LightCoral"  CornerRadius="10" Margin="0,0.2,0,0.2">
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CommonStates">
                                <VisualStateGroup.Transitions>
                                    <VisualTransition To="MouseOver" GeneratedDuration="0:0:0.1" />
                                </VisualStateGroup.Transitions>
                                <VisualState x:Name="Normal"/>
                                <VisualState x:Name="MouseOver">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="LayoutBorder" Storyboard.TargetProperty="BorderBrush">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="#1BA1E2"/>
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="Disabled">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="LayoutRoot" Storyboard.TargetProperty="Background">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="Gray"/>
                                        </ObjectAnimationUsingKeyFrames>
                                        <DoubleAnimation Storyboard.TargetName="LayoutRoot" Storyboard.TargetProperty="Opacity" Duration="0" To=".5" />
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                            <VisualStateGroup x:Name="SelectionStates">
                                <VisualState x:Name="Unselected"/>
                                <VisualState x:Name="Selected">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="LayoutBorder" Storyboard.TargetProperty="BorderBrush">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="#1BA1E2"/>
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="LayoutBorder" Storyboard.TargetProperty="BorderThickness">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="0.6"/>
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="NameTextBlock" Storyboard.TargetProperty="Foreground">
                                            <DiscreteObjectKeyFrame KeyTime="0"  Value="#E09728"/>
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="RepostTextBlock" Storyboard.TargetProperty="Foreground">
                                            <DiscreteObjectKeyFrame KeyTime="0"  Value="#1BA1E2"/>
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="CommentTextBlock" Storyboard.TargetProperty="Foreground">
                                            <DiscreteObjectKeyFrame KeyTime="0"  Value="#1BA1E2"/>
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="FriendBorder" Storyboard.TargetProperty="BorderBrush">
                                            <DiscreteObjectKeyFrame KeyTime="0"  Value="#1BA1E2"/>
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="64" />
                                <ColumnDefinition Width="80" />
                                <ColumnDefinition Width="130" />
                                <ColumnDefinition Width="170"  />
                            </Grid.ColumnDefinitions>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="37" />
                                <RowDefinition Height="37" />
                                <RowDefinition Height="*" />
                                <RowDefinition Height="*" />
                                <RowDefinition Height="*" />
                            </Grid.RowDefinitions>

                            <Border Grid.Column="0" Grid.Row="0" Grid.RowSpan="2" x:Name="FriendBorder" BorderThickness="2" Margin="10,10,0,10" CornerRadius="5">
                                <Image Height="50" Width="50"  HorizontalAlignment="Center" VerticalAlignment="Top" Source="{Binding ImageUrl, Mode=OneWay}" >
                                    <Image.Clip>
                                        <RectangleGeometry RadiusX="5" RadiusY="5" Rect="0, 0, 50, 50" />
                                    </Image.Clip>
                                </Image>
                            </Border>

                            <TextBlock x:Name="NameTextBlock" Grid.ColumnSpan="2" Grid.Row="0" Grid.Column="1" Margin="10,6,0,0" Text="{Binding ScreenName, Mode=OneWay}" Style="{StaticResource NameTextBlockStyle}" />
                            <TextBlock x:Name="DateTextBlock" Grid.Row="0" Grid.Column="3" Margin="0,6,0,0" Text="{Binding Date, Mode=OneWay}" Style="{StaticResource MidelTextBlock}" />

                            <TextBlock x:Name="RepostTextBlock" Grid.Row="1" Grid.Column="1" Margin="10,6,0,0" Text="{Binding RepostNumb, Mode=OneWay}" Style="{StaticResource SmallTextBlock}"/>
                            <TextBlock x:Name="CommentTextBlock" Grid.Row="1" Grid.Column="2" Margin="10,6,0,0"  Text="{Binding CommentNumb, Mode=OneWay}" Style="{StaticResource SmallTextBlock}" />
                            <TextBlock x:Name="SourceTextBlock" Grid.Row="1" Grid.Column="3" Margin="0,3,0,0" Text="{Binding Source, Mode=OneWay}" Style="{StaticResource MidelTextBlock}" />
                            <TextBlock Grid.Column="0" Grid.ColumnSpan="4" Grid.Row="2" Width="440" Text="{Binding Content, Mode=OneWay}" TextWrapping="Wrap" Margin="0,5,0,2" Style="{StaticResource PhoneTextSubtleStyle}" />
                            <Border Visibility="{Binding IsBusy}" Grid.Column="0" Grid.ColumnSpan="4" Grid.Row="3" Background="Gray" Width="420" Padding="5" CornerRadius="15,18,30,10" BorderThickness="2, 3, 1, 0.5" BorderBrush="Chocolate">
                                <TextBlock x:Name="OriginalTextBlock" Width="400" Margin="0,2,0,5" Text="{Binding Original, Mode=OneWay}" TextWrapping="Wrap" Style="{StaticResource PhoneTextSubtleStyle}" />
                            </Border>
                            <Image Grid.Column="0" Grid.ColumnSpan="4" Grid.Row="4" Stretch="None" Margin="0,2,0,2" HorizontalAlignment="Center" VerticalAlignment="Center" Source="{Binding ImageUrl}" >
                            </Image>
                        </Grid>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
4

3 回答 3

1

考虑将一些工作推到一个线程中,使用 Observable 集合并尝试在 UI 中使用虚拟化列表来帮助解决一些速度问题。

这对我有点帮助。 http://blog.landdolphin.net/?p=78

于 2011-06-03T05:41:41.440 回答
0

有时我会看 Silverlight TV 并找到一个很好的提示。

滚动时不要让任何图像下载发生:长列表框上的图像下载应由 ScrollViewer 状态和位置控制以优化网络活动,并且在基于队列的后台线程系统中下载图像也是有效的。这将通过仅下载可见图像集来确保滚动性能,这也仅在滚动活动停止时才下载。

检查博客文章。

于 2011-06-02T17:46:12.317 回答
0

从 UI 线程加载图像将有助于提高性能。一个简单的方法是使用David Anson 的 LowProfileImageLoader

Peter Torr 创建了一个 LazyListBox,它在滚动时使用了一个简化的模板。一定要看看这篇文章。

在查看您的模板时,ListBoxItem 似乎非常复杂。我强烈建议您重新考虑是否可以使用更简单的模板(也许将 UI 中的一些文本组合起来,以便一次绑定多个字段)和/或减少您为每个项目显示的信息量。也许将一些细节移到另一个页面。

于 2011-06-02T20:03:16.077 回答