问题
I have had a look through similar errors, but cannot find one matching my scenario.
I am using the 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 receive 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 am doing is populating a Map in Windows Phone 8. When it works, it is fine. I have 25 items in my list which are added as pushpins to the 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>
C#
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 Geocoordinates.
回答1:
The moment you set ItemsSource, Items becomes read only. You will have to pick which one you want to use. You can't mix and match here. So before setting ItemsSource, call Items.Clear()
回答2:
AFAIK you can't have databound items and have hardcoded items for the same ItemsControl. That means that your hardcoded UserLocationMarker isn't going to work there as long as you're using DataBinding.
回答3:
I've been looking for a solution to this problem and as I've checked the source code of the Extension it turned out that when ItemsSource is changing, there is an if statement that checks if Items.Count > 0 and throws exception.
So to set new Collection as ItemsSource you can use such a 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
回答4:
I had a problem with clearing the ItemsSource List. The solution in my case was using Clear on the Items but not to forget to set ItemsSource = null because that triggers it. Then you can set a new value to ItemsSource. Off course this must be done in a Dispatcher block because it runs on the UI thread.
来源:https://stackoverflow.com/questions/14525618/phone-maps-items-collection-must-be-empty-before-using-itemssource