我为windows phone 7(bing maps)创建应用程序,我需要添加如下内容:
我该怎么做?或者我在哪里可以找到一些教程?
答案 0 :(得分:0)
您可以订阅地图的Tap事件并添加Pushpins。
XAML:
<Maps:Map x:Name="Map" CredentialsProvider="Your-Creds" Tap="Map_OnTap" />
代码:
private void Map_OnTap(object sender, GestureEventArgs e)
{
GeoCoordinate location;
if (Map.TryViewportPointToLocation(e.GetPosition(Map), out location))
{
Map.Children.Add(new Pushpin
{
Location = location,
Background = new SolidColorBrush(Colors.Blue),
Content = "Hello world"
});
}
}
如果您想要MVVM方法,可以添加一组您添加的对象。
的Xaml:
<Maps:Map x:Name="Map" CredentialsProvider="Your-creds"
Tap="Map_OnTap">
<Maps:MapItemsControl ItemsSource="{Binding Locations}">
<Maps:MapItemsControl.ItemTemplate>
<DataTemplate>
<Maps:Pushpin Location="{Binding Location}" Content="{Binding Name}"
Background="{StaticResource PhoneAccentBrush}">
</Maps:Pushpin>
</DataTemplate>
</Maps:MapItemsControl.ItemTemplate>
</Maps:MapItemsControl>
</Maps:Map>
代码:
public ObservableCollection<MapLocation> Locations { get; private set; }
private void Map_OnTap(object sender, GestureEventArgs e)
{
GeoCoordinate location;
if (Map.TryViewportPointToLocation(e.GetPosition(Map), out location))
{
Locations.Add(new MapLocation
{
Location = location,
Name = "Hello world"
});
}
}
public class MapLocation
{
public GeoCoordinate Location { get; set; }
public string Name { get; set; }
}
此示例只是将页面的DataContext设置为自身。更好的方法是将页面的DataContext设置为具有Locations属性的视图模型。