MapsItemControl 和 ObservableCollection 行为

我有下一个 MapsItemControl 模板来显示地图上的许多图钉/项目。它绑定到 ObservableCollection,因为我希望它们被过滤以通过不同的选项显示或隐藏。这是 Map 控件内 MapsItemControl 的 XAML 代码。


<Maps:MapItemsControl x:Name="mapSpotsItems">

    <Maps:MapItemsControl.ItemTemplate>

        <DataTemplate>

            <StackPanel x:Name="spotPin" Visibility="{Binding isVisible}"

                        Maps:MapControl.Location="{Binding geopoint}" Tag="{Binding ID}" ToolTipService.ToolTip="{Binding Description}" 

                        RenderTransformOrigin="0.5,1" Tapped="spotPin_Tapped">



                 <StackPanel Orientation="Horizontal" Tag="{Binding ID}">

                     <Grid>

                         <!-- Karratua -->

                         <Rectangle Width="25" Height="25" Fill="{StaticResource DarkGreyThemeColor}" Opacity="0.5"/>

                         <Rectangle Width="25" Height="25" Fill="{x:Null}" Stroke="Black" StrokeThickness="0.5" />

                         <!-- Borobila -->

                         <Image Source="{Binding MainTag}" Height="20" Width="20" HorizontalAlignment="Center" VerticalAlignment="Center"/>

                      </Grid>


                      <StackPanel Background="{StaticResource DarkGrey75ThemeColor}">

                          <TextBlock Text="{Binding Title}" Margin="5,0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Width="50" MaxLines="2" TextWrapping="Wrap" TextTrimming="CharacterEllipsis" Style="{StaticResource BaseTextBlockStyle}" FontSize="11" Foreground="{StaticResource LightGreyThemeColor}" LineHeight="11"/>

                       </StackPanel>

                   </StackPanel>


                   <Rectangle Width="2" Height="10" Fill="Black" StrokeThickness="0" StrokeEndLineCap="Triangle" />


               </StackPanel>

           </DataTemplate>

       </Maps:MapItemsControl.ItemTemplate>

   </Maps:MapItemsControl>

我有一些与之相关的问题。



长风秋雁
浏览 89回答 1
1回答

慕慕森

当我更改 ObservableCollection 时,例如 isVisible 值(通过过滤器中的选择显示或隐藏,项目根本没有改变。Visibility是Enum但不是bool。您需要BoolToVisConverter为 xaml 制作。public class BoolToVisConverter : IValueConverter{&nbsp; &nbsp; public object Convert(object value, Type targetType, object parameter, string language)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return (value is bool && (bool)value) ? Visibility.Visible : Visibility.Collapsed;&nbsp; &nbsp; }&nbsp; &nbsp; public object ConvertBack(object value, Type targetType, object parameter, string language)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return value is Visibility && (Visibility)value == Visibility.Visible;&nbsp;&nbsp; &nbsp; }}项目的创建(我有 2000-3000 个项目)非常缓慢,并且在平移地图时移动有明显的滞后。有什么方法可以让它更愉快和响应更迅速吗?MapItemsControl与ListViewand不同GridView,它不支持UI虚拟化。为了性能,请避免一次渲染太多项目最后一个问题,尽管我设置了 RenderTransformOrigin="0.5,1",但它就像我设置了 RenderTransformOrigin="0.0,0.0",从左上边界渲染。RenderTransformOrigin属性不用于设置AnchorPoint。要设置正确的布局,您可以设置NormalizedAnchorPoint.<maps:MapItemsControl.ItemTemplate>&nbsp; &nbsp; <DataTemplate>&nbsp; &nbsp; &nbsp; &nbsp; <Button x:Name="mapItemButton" Click="mapItemButton_Click" Background="Transparent">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <StackPanel>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Border Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <TextBlock Text="{Binding DisplayName}"/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </Border>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Image Source="{Binding ImageSourceUri}"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;maps:MapControl.NormalizedAnchorPoint="{Binding NormalizedAnchorPoint}"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;maps:MapControl.Location="{Binding Location}">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Image.Transitions>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <TransitionCollection>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <EntranceThemeTransition/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </TransitionCollection>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </Image.Transitions>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </Image>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </StackPanel>&nbsp; &nbsp; &nbsp; &nbsp; </Button>&nbsp; &nbsp; </DataTemplate></maps:MapItemsControl.ItemTemplate>更多细节请参考官方代码示例。
打开App,查看更多内容
随时随地看视频慕课网APP