将ObservableCollection中的Pushpin绑定到Bing Maps Control WP7

时间:2011-12-30 21:39:47

标签: c# .net silverlight windows-phone-7 bing-maps

我正在开发一个Windows Phone 7项目(芒果),我正在尝试使用ObservableCollection将Pushpins绑定到我的silverlight Bing Maps控件,但它无法正常工作。我在这里待了最近5个小时,在互联网上搜索了有关stackoverflow的其他问题,并且已经实现了一些答案,但找不到一个有效的方法:(

我会非常感谢任何想法,为什么它不起作用。我很确定它与我的XAML有关,因为我的ObservableCollection正确填充(在运行时使用断点检查)和有效的Locations。目前我的ObservableCollection只填充了两个位置,但是当我开始使用Bing RouteService时,我会希望增加这个数字。

以下是代码:

public partial class MapView : PhoneApplicationPage
{
    private readonly CredentialsProvider bingMapsCredentials = new ApplicationIdCredentialsProvider(App.BingMapsKey);
    private GeoCoordinate geoDestination;
    private GeoCoordinate geoCurrentLocation;
    public ObservableCollection<PushpinModel> PushpinCollection { get; set; }

    public MapView()
    {
        InitializeComponent();

        geoDestination = new GeoCoordinate(54.975556, -1.621667);
        geoCurrentLocation = new GeoCoordinate(53.463056, -2.291389);

        CreatePushpins();
    }

    private void CreatePushpins()
    {
        PushpinModel currentLocationModel = new PushpinModel();
        PushpinModel destinationLocationModel = new PushpinModel();

        currentLocationModel.Location = geoCurrentLocation;
        destinationLocationModel.Location = geoDestination;

        PushpinCollection = new ObservableCollection<PushpinModel>();
        PushpinCollection.Add(currentLocationModel);
        PushpinCollection.Add(destinationLocationModel);
    }

下面是PushpinModel类:

using System.Device.Location;

namespace NavigationApp
{
    public class PushpinModel
    {
       public GeoCoordinate Location { get; set; }
    }
}

下面是违规的XAML(我想!):

<phone:PhoneApplicationPage 
    x:Class="NavigationApp.MapView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:NavigationApp"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait" Orientation="Portrait"
    mc:Ignorable="d" d:DesignHeight="768" d:DesignWidth="480"
    shell:SystemTray.IsVisible="True" 
    xmlns:my="clr-namespace:Microsoft.Phone.Controls.Maps;assembly=Microsoft.Phone.Controls.Maps">
    <phone:PhoneApplicationPage.Resources>
        <local:PushpinModel x:Key="PushpinModel" />
        <DataTemplate x:Key="LogoTemplate">
            <my:Pushpin Location="{Binding Location}" Background="#FFB6DE2E" />
        </DataTemplate>
    </phone:PhoneApplicationPage.Resources>

    <!--LayoutRoot is the root grid where all page content is placed-->
    <Grid x:Name="LayoutRoot" Background="Transparent">
        <Grid.RowDefinitions>
            <RowDefinition Height="0"/>
            <RowDefinition Height="768*"/>
        </Grid.RowDefinitions>

        <!--TitlePanel contains the name of the application and page title-->
        <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28"></StackPanel>

        <!--ContentPanel - place additional content here-->
        <Grid x:Name="ContentPanel" Grid.Row="1">
            <my:Map Height="520" HorizontalAlignment="Left" Margin="6,6,0,0" Name="map1" VerticalAlignment="Top" Width="468" ZoomBarVisibility="Collapsed" ZoomLevel="1" CredentialsProvider="{Binding bingMapsCredentials}" >
                <my:MapItemsControl Name="Pushpinsss" ItemTemplate="{StaticResource LogoTemplate}" ItemsSource="{Binding PushpinCollection}" >
                 </my:MapItemsControl>
            </my:Map>
        </Grid>
    </Grid>    
</phone:PhoneApplicationPage>

如果您还需要Code / Info,请告诉我们:) 谢谢 莱恩

解决方案

将ObservableCollection更改为:

        private ObservableCollection<PushpinModel> PushpinCollection;
    public ObservableCollection<PushpinModel> pushpinCollection
    {
        get
        {
            return PushpinCollection;
        }
    }

XAML现在是:

 <my:MapItemsControl Name="Pushpinsss" ItemTemplate="{StaticResource LogoTemplate}" ItemsSource="{Binding pushpinCollection}" >

1 个答案:

答案 0 :(得分:1)

从您的代码中,您忘记设置DataContext。你可以这样做:

public MapView()
{
    InitializeComponent();

    geoDestination = new GeoCoordinate(54.975556, -1.621667);
    geoCurrentLocation = new GeoCoordinate(53.463056, -2.291389);

    CreatePushpins();
    DataContext = this;
}

原因是,您只能绑定到属性。所以这不起作用:

private readonly CredentialsProvider bingMapsCredentials = 
    new ApplicationIdCredentialsProvider(App.BingMapsKey);

XAML:

<my:Map ...  CredentialsProvider="{Binding bingMapsCredentials}" ... />

请改用包装器:

private readonly CredentialsProvider bingMapsCredentials = 
    new ApplicationIdCredentialsProvider(App.BingMapsKey);

public CredentialsProvider BingMapsCredentials 
{ 
     get { return bingMapsCredentials; } 
}

XAML:

<my:Map ...  CredentialsProvider="{Binding BingMapsCredentials}" ... />

关于DataBinding on MSDN有一个很好的概述(它是关于WPF的,但大部分也适用于WP7)