Phone.Maps-在使用ItemsSource之前,Items集合必须为空

时间:2013-01-25 16:07:05

标签: c# .net windows-phone-8

我查看了类似的错误,但找不到符合我情景的内容。

我正在使用此处的示例: http://wp.qmatteoq.com/maps-in-windows-phone-8-and-phone-toolkit-a-winning-team-part-2/

经常,但不是每次..我收到以下例外:

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.

堆栈跟踪:

   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)

我正在做的是在Windows Phone 8中填充Map。当它工作时,它很好。我的列表中有25个项目作为图钉添加到列表中。

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'是一个包含变量的List,包括Geocoordinates。

4 个答案:

答案 0 :(得分:2)

设置ItemsSource的那一刻,Items变为只读。你必须选择你想要使用的那个。你不能在这里混搭。因此,在设置ItemsSource之前,请调用Items.Clear()

答案 1 :(得分:1)

AFAIK您不能拥有数据绑定项目并且具有相同ItemsControl的硬编码项目。这意味着只要您使用DataBinding,您的硬编码UserLocationMarker就不会在那里工作。

答案 2 :(得分:1)

我一直在寻找这个问题的解决方案,因为我检查了扩展的源代码,结果发现当ItemsSource发生变化时,有一个if语句检查Items.Count&gt; 0并抛出异常。

因此,要将新Collection设置为ItemsSource,您可以使用以下代码:

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

答案 3 :(得分:0)

我在清除ItemsSource列表时遇到问题。我的案例中的解决方案是在项目上使用Clear但不要忘记设置ItemsSource = null,因为它会触发它。然后,您可以为ItemsSource设置新值。当然,这必须在Dispatcher块中完成,因为它在UI线程上运行。