有一个包含以下ItemContainerStyle的列表框,它绑定到ViewModel类,但滚动列表框时其性能不顺畅。我在网上查看了这个,然后我用listbox中的普通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>
答案 0 :(得分:1)
考虑将一些工作推送到一个线程中,使用Observable集合并尝试在UI中使用虚拟化列表来帮助解决一些速度问题。
这对我有所帮助。 http://blog.landdolphin.net/?p=78
答案 1 :(得分:0)
有时我会看Silverlight电视并找到一个很好的提示。
滚动时不要让任何图片下载:图片 在长列表框上下载应该 由ScrollViewer控制 状态和位置优化 网络活动也是如此 有效下载图像 基于队列的后台线程系统。 这将确保滚动性能 通过只下载可见的一组 图像,那也只有当 滚动活动停止。
查看this博文。
答案 2 :(得分:0)
从UI线程加载图像有助于提高性能。一个简单的方法是使用LowProfileImageLoader from David Anson。
Peter Torr has created a LazyListBox其中包括在滚动时使用简化模板。绝对检查这篇文章。
在查看模板时,ListBoxItem似乎非常复杂。我强烈建议您重新考虑是否可以使用更简单的模板(可能会将UI中的某些文本组合在一起,以便可以同时绑定多个字段)和/或减少您为每个项目显示的信息量。也许将一些细节移到另一页。