2011-12-30 229 views
1

我正在一個Windows Phone 7項目(芒果),並試圖綁定到我的silverlight Bing Maps控件使用ObservableCollection,但它不會工作。我一直坐在這裏過去的5個小時,在互聯網上搜索和其他問題,並已實施一些答案,但找不到一個工作:(綁定在ObservableCollection圖釘Bing地圖控制WP7

我會非常感謝任何想法,爲什麼因爲我的ObservableCollection被正確填充(在運行時使用斷點檢查)和有效的位置,所以我很確定它和我的XAML一起工作。目前,我的ObservableCollection只填充了兩個位置,但是我會期待增加這個數字,當我開始使用必應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> 

如果需要再代碼/信息,然後才讓我知道:) 謝謝 瑞安

解決方案

更改的ObservableCollection到:

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

和XAML現在是:

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

回答

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(這是關於W PF但大部分也適用於WP7)

+0

非常感謝您的快速回復,Bing地圖憑據實際上工作正常,但是我已採取了您對封裝屬性的說明,並已實施爲我的ObservableCollection,並設置數據上下文,它的出色工作! 請參閱原始帖子的「解決方案」部分,瞭解修改的XAML和C# – ry8806 2011-12-30 23:36:36