Windows Phone 8.1中地图上的绑定位置

时间:2014-10-16 08:14:17

标签: c# windows-phone-8.1

我正在尝试将图片绑定到Windows Phone 8.1中的地图上的位置:

<Maps:MapControl x:Name="RestoMap" MapServiceToken="" Height="520" Margin="0,50,0,0" Width="380" ZoomLevel="8">
        <Maps:MapItemsControl x:Name="MapIcons" >
            <Maps:MapItemsControl.ItemTemplate>
                <DataTemplate>
                    <StackPanel Background="DarkSlateBlue" Width="170" Height="170">
                        <Image Margin="0,0,0,0" HorizontalAlignment="Center" Source="{Binding picture}" Width="150" Height="100" Maps:MapControl.Location="{Binding location}"/>
                    </StackPanel>
                </DataTemplate>
            </Maps:MapItemsControl.ItemTemplate>
        </Maps:MapItemsControl>
    </Maps:MapControl>

代码:图片没有出现在地图上任何帮助

{
double latitude = ....;
double longitude = ....;
Location location = new Location();
location.Latitude = latitude;
location.Longitude = longitude;
locations.Add(location);
}
MapIcons.ItemsSource = this.locations;

1 个答案:

答案 0 :(得分:0)

绑定错误。

根据您的XAML,该课程应该包含以下两个属性,一个是picture来显示图片,另一个是location来设置图像位置地图。我将它们重命名为MapPictureMapLocation以遵循CamelCase命名约定。

班级:

class MapIcon
{
    public ImageSource MapPicture { get; set;}
    public Location MapLocation { get; set; }
} 

和更新的XAML - 只重命名2个属性:

<DataTemplate>
    <StackPanel Background="DarkSlateBlue" Width="170" Height="170">
        <Image Margin="0,0,0,0" HorizontalAlignment="Center" Source="{Binding MapPicture}" Width="150" Height="100" Maps:MapControl.Location="{Binding MapLocation}"/>
    </StackPanel>
</DataTemplate>

以及如何将一组MapIcon绑定到MapItemsControl

ObservableCollection<MapIcon> icons = new ObservableCollection<MapIcon>();  

private void ShowMapIcons()
{
    BitmapImage img = new BitmapImage(....);
    double latitude = ....;
    double longitude = ....;
    Location location = new Location();
    location.Latitude = latitude;
    location.Longitude = longitude;
    MapIcon icon = new MapIcon() { MapLocation = location,  ImageSource = img };

    icons.Add(icon);

    MapIcons.ItemsSource = icons;
}