Phone.Maps. Collection item must be empty before using ItemsSource

I looked at similar errors, but I can not find one that matches my scenario.

I use an example from here: http://wp.qmatteoq.com/maps-in-windows-phone-8-and-phone-toolkit-a-winning-team-part-2/

Quite often, but not every time .. I get the following exception:

An exception of type 'System.InvalidOperationException' occurred in Microsoft.Phone.Controls.Toolkit.DLL but was not handled in user code Items collection must be empty before using ItemsSource. 

Stacktrace:

  at Microsoft.Phone.Maps.Toolkit.MapItemsControl.OnItemsSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) at System.Windows.DependencyObject.RaisePropertyChangeNotifications(DependencyProperty dp, Object oldValue, Object newValue) at System.Windows.DependencyObject.UpdateEffectiveValue(DependencyProperty property, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, ValueOperation operation) at System.Windows.DependencyObject.SetValueInternal(DependencyProperty dp, Object value, Boolean allowReadOnlySet) at System.Windows.DependencyObject.SetValue(DependencyProperty dp, Object value) at Microsoft.Phone.Maps.Toolkit.MapItemsControl.set_ItemsSource(IEnumerable value) at NextBuses.MainPage.GetMembersCompleted(Object sender, GetMembersCompletedEventArgs e) at NextBuses.SQLService.Service1Client.OnGetMembersCompleted(Object state) 

What I'm doing is filling out the card in Windows Phone 8. When it works, everything is fine. I have 25 items in my list that are added to the contact list.

XAML:

 <my:Map Height="696" MouseLeftButtonDown="Close_popup" HorizontalAlignment="Left" Name="map1" VerticalAlignment="Top" Width="480" Grid.RowSpan="2" ZoomLevel="5.5" > <toolkit:MapExtensions.Children> <toolkit:UserLocationMarker x:Name="UserLocationMarker" Visibility="Visible" /> <toolkit:MapItemsControl > <toolkit:MapItemsControl.ItemTemplate> <DataTemplate> <toolkit:Pushpin MouseLeftButtonUp="pin_click" GeoCoordinate="{Binding Location1}" Template="{StaticResource PushpinControlTemplate1}"/> </DataTemplate> </toolkit:MapItemsControl.ItemTemplate> </toolkit:MapItemsControl> </toolkit:MapExtensions.Children> </my:Map> 

WITH#

  ObservableCollection<DependencyObject> children = MapExtensions.GetChildren(map1); var obj = children.FirstOrDefault(x => x.GetType() == typeof(MapItemsControl)) as MapItemsControl; obj.ItemsSource = details; 

'details' is a list with variables in it, including geo-coordinates.

+4
source share
4 answers

The moment you set the ItemsSource, the items become read-only. You will need to choose the one you want to use. You cannot mix and match here. Therefore, before setting ItemsSource, call Items.Clear ()

+2
source

AFAIK, you cannot have data items and have hardcoded items for a single ItemsControl. This means that your hardcoded UserLocationMarker will not work there while you are using DataBinding.

+1
source

I was looking for a solution to this problem, and since I checked the source code of the extension, it turned out that when changing ItemsSource, there is an if statement that checks if Items.Count> 0 and throws an exception.

So, to set the new collection as ItemsSource, you can use this code:

 MapItemsControl MIC = MapExtensions.GetChildren(map1).FirstOrDefault(x => x is MapItemsControl) as MapItemsControl; if (MIC != null && MIC.ItemsSource != null) { (MIC.ItemsSource as IList).Clear() // clear old collection MIC.ItemsSource = null; } MIC.ItemsSource = details; // new collection 
+1
source

I am having trouble clearing the ItemsSource list. The solution in my case was to use Clear on the Items, but not to forget to set ItemsSource = null, because it calls it. Then you can set the new value in ItemsSource. Of course, this should be done in the dispatcher block, because it works in the user interface thread.

0
source

Source: https://habr.com/ru/post/952982/


All Articles