正如您提到的,
Location
和
Title
可以绑定到
Geopoint
和
AttractionName
。但是要在地图控件上显示MapIcon,我们需要通过编程将MapIcon添加到其MapElements集合中。
例如:
在
MapControl
的
Loaded
事件中。
private void MapControl_Loaded(object sender, RoutedEventArgs e)
{
MapControl.MapElements.Add(MyMapIcon);
}
这之后,MapIcon
就可以像上面的图片一样显示在 MapControl
上。但是你会发现,在 MapControl
的左上角有一个类名的字符串。
这是因为你将 MapIcon
作为 MapControl
的隐式子元素添加进去了。这相当于在 XAML 代码中像下面这样将 MapIcon
添加到 MapItemsControl
中。
<Maps:MapControl x:Name="MapControl" ZoomLevel="14" Center="{Binding Geopoint, Mode=OneWay}" Margin="-12,0,-12,0" Height="200" PanInteractionMode="Disabled" RotateInteractionMode="Disabled" Loaded="MapControl_Loaded">
<Maps:MapItemsControl>
<Maps:MapIcon x:Name="MyMapIcon" Location="{Binding Geopoint}" Title="{Binding AttractionName}" />
</Maps:MapItemsControl>
</Maps:MapControl>
MapItemsControl可以通过将XAML用户界面元素(如Button、HyperlinkButton或TextBlock)作为MapControl的子项添加来显示它们。MapIcon不能在MapControl上显示,因此它会显示其类名。
作为解决方法,我们可以将MapIcon
放在Resources
中来解决这个问题。
例如:
<Page.Resources>
<Maps:MapIcon x:Name="MyMapIcon" Location="{Binding Geopoint}" Title="{Binding AttractionName}" />
</Page.Resources>
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Maps:MapControl x:Name="MapControl" ZoomLevel="14" Center="{Binding Geopoint, Mode=OneWay}" Margin="-12,0,-12,0" Tapped="directions_Click" Height="200" MapServiceToken="{StaticResource BingMapsKey}" PanInteractionMode="Disabled" RotateInteractionMode="Disabled" Loaded="MapControl_Loaded">
</Maps:MapControl>
</Grid>