2014-03-29 20 views
1

我嘗試添加多個圖釘來映射。如何從代碼隱藏(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異常。怎麼了?

+0

您是否嘗試過調試並查看引發異常的位置以及變量的內容是什麼? – Romasz

+0

@Romasz,你能告訴我該怎麼做才能找到引發異常的地方? 順便說一句,signalsWithCoords變量不是null,它包含193個元素。 – splash27

+0

我發現PushpinCollection爲null,它會拋出異常。但爲什麼發生這種情況PushpinCollection是一個x:XAML中的MapItemsControl的名稱。 – splash27

回答

1

正如我已經檢查PushpinCollection存在(不爲null)和下面的代碼應工作:

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);      
     } 
    } 
    MapItemsControl PushpinCol = MapExtensions.GetChildren(map1).FirstOrDefault(x => x is MapItemsControl) as MapItemsControl; 

    PushpinCol.ItemsSource = signalsWithCoords; 
} 

挖遠一點事實證明,這似乎是一個小錯誤。在源代碼DependencyProperty NameProperty註冊爲"ItemTemplate" - 這可能會阻止從代碼名稱獲取它。

編輯 - 評論

挖在源代碼中多了幾分之後brigns,這種異常是由MapExtension時Items.Count > 0並嘗試改變的ItemsSource拋出的信息。這裏有一個方法可以讓你改變ItemsSource:

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);      
     } 
    } 
    MapItemsControl PushpinCol = MapExtensions.GetChildren(map1).FirstOrDefault(x => x is MapItemsControl) as MapItemsControl; 
    if (PushpinCol != null && PushpinCol.ItemsSource != null) 
    { 
     (PushpinCol.ItemsSource as IList).Clear(); 
     PushpinCol.ItemsSource = null; 
    } 
    PushpinCol.ItemsSource = signalsWithCoords; 
} 

另一方面考慮讓你的Collection靜態並將其設置爲ItemsSource一次。

+0

順便說一句,如何提取與圖釘綁定的對象?我使用MouseLeftButtonUp事件,但我無法將發件人對象轉換爲IssueDescription。 – splash27

+0

@ splash27你試過了:(發件人爲Pushpin).DataContext爲IssueDescription)? – Romasz

+0

謝謝,幫助=) – splash27