我嘗試添加多個圖釘來映射。如何從代碼隱藏(WP8)設置多個圖釘?
這裏的XAML:
<maps:Map ZoomLevel="8" Height="500" x:Name="map1" Width="415" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="0,0,0,0">
<toolkit1:MapExtensions.Children>
<toolkit1:MapItemsControl x:Name="PushpinCollection">
<toolkit1:MapItemsControl.ItemTemplate>
<DataTemplate>
<toolkit1:Pushpin GeoCoordinate="{Binding Coords}" Content="{Binding Name}"/>
</DataTemplate>
</toolkit1:MapItemsControl.ItemTemplate>
</toolkit1:MapItemsControl>
</toolkit1:MapExtensions.Children>
</maps:Map>
下面的代碼:
protected override async void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
var xmlDataSource = new XmlDataSource();
var sigCol = new SignalCollection(xmlDataSource);
var allSignals = await sigCol.GetData(false, false, "UA");
ObservableCollection<IssueDescription> signalsWithCoords = new ObservableCollection<IssueDescription>();
foreach (var signal in allSignals)
{
if (signal.Coords != null)
{
signalsWithCoords.Add(signal);
}
}
PushpinCollection.ItemsSource = signalsWithCoords;
}
但最後一個字符串拋出NullReferenceException異常。怎麼了?
您是否嘗試過調試並查看引發異常的位置以及變量的內容是什麼? – Romasz
@Romasz,你能告訴我該怎麼做才能找到引發異常的地方? 順便說一句,signalsWithCoords變量不是null,它包含193個元素。 – splash27
我發現PushpinCollection爲null,它會拋出異常。但爲什麼發生這種情況PushpinCollection是一個x:XAML中的MapItemsControl的名稱。 – splash27