C# Phone.Maps-在使用ItemsSource之前,项目集合必须为空
我已经查看了类似的错误,但找不到与我的场景匹配的错误 我使用的例子如下: 经常,但不是每次。。我收到以下例外情况:C# Phone.Maps-在使用ItemsSource之前,项目集合必须为空,c#,.net,windows-phone-8,C#,.net,Windows Phone 8,我已经查看了类似的错误,但找不到与我的场景匹配的错误 我使用的例子如下: 经常,但不是每次。。我收到以下例外情况: 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 ItemsSou
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)
我正在做的是在WindowsPhone8中填充地图。当它起作用时,它是好的。我的列表中有25个项目作为图钉添加到列表中
XAML:
C#
observateCollection children=MapExtensions.GetChildren(map1);
var obj=children.FirstOrDefault(x=>x.GetType()==typeof(MapItemsControl))作为MapItemsControl;
obj.ItemsSource=详细信息;
“详细信息”是一个包含变量的列表,其中包括地理坐标。设置项目资源时,项目将变为只读。你必须选择你想用的。你不能在这里混搭。因此,在设置ItemsSource之前,请调用Items.Clear()AFAIK,您不能为同一ItemsControl设置数据绑定项和硬编码项。这意味着,只要您使用数据绑定,硬编码的UserLocationMarker就无法在那里工作。我在清除ItemsSource列表时遇到问题。在我的例子中,解决方案是在项目上使用Clear,但不要忘记设置ItemsSource=null,因为这会触发它。然后可以为ItemsSource设置一个新值。当然,这必须在Dispatcher块中完成,因为它在UI线程上运行。我一直在寻找解决此问题的方法,并且在检查扩展的源代码时发现,当ItemsSource发生更改时,有一个if语句检查Items.Count>0并引发异常 因此,要将新集合设置为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
谢谢你的回复。你能把你的回答再详细一点吗?因为我不太明白如何绕开它?你知道为什么它只是偶尔发生吗?这种情况总是发生的,但在你的例子中,可能是因为有时候你的items集合在设置源时是空的(或者如果它不是空的,因为它被ItemsSource填充)。简单一点:永远不要直接修改.Items。将ItemsSource设置为集合,并始终修改此集合。Justin,如果删除UserlocationMaker控件,我仍然经常会出现错误。在此处查看源代码:连同此注释帮助我解决了问题-与此类似。我需要您的帮助。我也有同样的问题,当我试图清除这些项时,我得到一个异常:集合处于不可写模式。还尝试使用dispatcher:System.Windows.Deployment.Current.dispatcher.BeginInvoke(()=>{mapItemsControl.Items.Clear();mapItemsControl.ItemsSource=null;});但我得到了相同的异常。@Romasz我试图使用相同的代码,但在第
MIC.ItemsSource=null行得到了相同的异常代码>请帮助
ObservableCollection<DependencyObject> children = MapExtensions.GetChildren(map1);
var obj = children.FirstOrDefault(x => x.GetType() == typeof(MapItemsControl)) as MapItemsControl;
obj.ItemsSource = details;
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