[英]Binding Pushpins in ObservableCollection to Bing Maps Control WP7
我正在Windows Phone 7項目(芒果)上工作,並嘗試使用ObservableCollection將圖釘綁定到我的silverlight必應地圖控件,但無法正常工作。 我已經在這里坐了最后5個小時,在互聯網上搜尋關於stackoverflow的問題以及其他問題,並實現了一些答案,但找不到一個可行的方法:(
對於它為什么不起作用的任何想法,我將不勝感激。 我很確定這與XAML有關,因為我的ObservableCollection已使用有效的Location正確填充(在運行時使用斷點檢查)。 目前,我的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>
如果您需要更多的代碼/信息,請告訴我:)謝謝Ryan
解
將ObservableCollection更改為:
private ObservableCollection<PushpinModel> PushpinCollection;
public ObservableCollection<PushpinModel> pushpinCollection
{
get
{
return PushpinCollection;
}
}
XAML現在是:
<my:MapItemsControl Name="Pushpinsss" ItemTemplate="{StaticResource LogoTemplate}" ItemsSource="{Binding pushpinCollection}" >
從代碼中可以看出,您忘記了設置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}" ... />
在MSDN上有一個關於DataBinding的很好的概述(關於WPF,但大部分也適用於WP7)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.