问题是我不能一次看到所有 20.000 个 MapIcon,我想进一步增加 MapIcon 的数量。
我的ViewModel 中调用了一个System.Collections.ObjectModel.ObservableCollection<House>
道具Houses
HousesViewModel
public MainPage()
{
this.InitializeComponent();
HousesVM.Houses.CollectionChanged += Houses_CollectionChangedAsync;
GetInitialPins();
}
我GetInitialPins()
有一个 foreach 来添加/填充 HousesVm.Houses 但这似乎效率低下,所以我像这样重写了它:
List<House> houses = JsonConvert.DeserializeObject<List<House>>(response);
HousesVM.Houses = new System.Collections.ObjectModel.ObservableCollection<House>(houses);
缺点是这不会触发CollectionChanged
我添加 MapIcons 的事件。
private async void Houses_CollectionChangedAsync(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
var dispatcher = CoreApplication.MainView.CoreWindow.Dispatcher;
await dispatcher.RunAsync(CoreDispatcherPriority.Low, async () =>
{
foreach (House item in e.NewItems)
{
MapIcon myPOI = new MapIcon
{
Location = new Geopoint(new BasicGeoposition() { Latitude = item.Latitude, Longitude = item.Longitude }),
NormalizedAnchorPoint = normalizedAnchorPoint,
Title = item.Name,
CollisionBehaviorDesired = MapElementCollisionBehavior.RemainVisible,
ZIndex = 0
};
await mappie.Dispatcher.RunAsync(CoreDispatcherPriority.Low, () =>
{
mappie.MapElements.Add(myPOI);
});
}
});
}
我也尝试过离开MapIcon
并添加Ellipse
到 mapcontrol 的子项中。这给了我一个内存不足的异常。还尝试在 XAML 中绑定:
<map:MapControl x:Name="mappie"
MapServiceToken="mysecrettoken"
Grid.Row="1">
<map:MapItemsControl ItemsSource="{Binding Houses}">
<map:MapItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock map:MapControl.Location="{Binding Location}" Text="{Binding Code}" map:MapControl.NormalizedAnchorPoint="0.5,0.5" FontSize="20" Margin="5"/>
</DataTemplate>
</map:MapItemsControl.ItemTemplate>
</map:MapItemsControl>
也给出了内存不足的异常... 也看到了 js 版的 bing maps 有集群/分组功能。uwp 中没有。并且在文档中还看到,我应该考虑构建一个磁贴服务并参考自定义创建的磁贴......这对我的情况来说似乎有点矫枉过正。