【问题标题】:"Items must be empty before using Items Source" when updating MapItemsControl.ItemsSource更新 MapItemsControl.ItemsSource 时,“使用项目源之前项目必须为空”
【发布时间】:2013-09-05 15:46:25
【问题描述】:

我有一个带有 MapsItemControl 的地图控件:

<maps:Map x:Name="MyMap">
    <maptk:MapExtensions.Children>
        <maptk:MapItemsControl>
            <maptk:MapItemsControl.ItemTemplate>
                <DataTemplate>
                    . . .
                </DataTemplate>
            </maptk:MapItemsControl.ItemTemplate>
        </maptk:MapItemsControl>
    </maptk:MapExtensions.Children>
</maps:Map>

我通过以下方式在代码中填充MapItemsControl

var itemCollection = MapExtensions.GetChildren((Map)MyMap).OfType<MapItemsControl>().FirstOrDefault();
itemCollection.ItemsSource = myItemCollection;

第一次将项目添加到地图时,这可以正常工作。但是如果我想用一个新的 soruce 项目集合来更新它,我会在itemCollection.ItemsSource = myItemCollection; 行中收到以下错误:

在使用项目源之前项目必须为空

所以,我尝试在代码中添加一行,以便在再次设置源之前删除项目,但没有成功:

var itemCollection = MapExtensions.GetChildren((Map)MyMap).OfType<MapItemsControl>().FirstOrDefault();
itemCollection.Items.Clear();
itemCollection.ItemsSource = myItemCollection;

现在我在itemCollection.Items.Clear(); 行中得到以下异常:

集合处于不可写模式

如何更新MapItemsControl 中的项目?

【问题讨论】:

  • 您是否在某个时候手动添加项目?通过使用itemCollection.Add(...)?

标签: c# xaml map windows-phone-8


【解决方案1】:

嗯……怎么样

itemCollection.Items = itemCollection.Items.Clear();

【讨论】:

    【解决方案2】:

    假设您继续使用相同的 myItemCollection 来保存您当前的信息(如果您正在搜索一个来源,这可能就是这种情况),那么您不需要重新绑定。相反,您需要抛出 PropertyChanged 事件,以便组件知道使用源的当前内容进行更新。有关更多信息,请参阅 Microsoft 的 Binding Sources tutorialHow to: Implement Property Change Notification

    【讨论】:

    • 不幸的是,绑定似乎不能与 MaptItemsControl.ItemsSource 一起使用。使用绑定将使我更容易实现我的目标。但在 WP8 中处理地图似乎比在 WP7 中更困难。
    • 根据 MapItemsControl (msdn.microsoft.com/en-us/library/jj620947.aspx) 页面,它是用于数据绑定的。它作为 ItemsControl (msdn.microsoft.com/en-us/library/…) 的子类执行此操作。
    • 这适用于 Windows 应用商店应用中的 Bing 地图。它也适用于 WP7 中的 Bing 地图,但不适用于 Windows Phone Toolkit 中的 MapItemsControl 和 WP8 中的诺基亚地图
    • 啊,好的。我没有抓住那个细节。
    【解决方案3】:

    尝试使用DataContext 属性。而且 afaik 您在使用时不需要编写 Clear 。试试这个:

    var itemCollection = MapExtensions.GetChildren((Map)MyMap).OfType<MapItemsControl>().FirstOrDefault();
    itemCollection.DataContext = myItemCollection;
    

    【讨论】:

    • itemCollection 没有 DataContext 属性。
    • 那么itemCollection.ItemsSource = null; itemCollection.ItemsSource = myItemCollection;要写什么呢?另外:stackoverflow.com/questions/14525618/…
    • 我得到同样的错误:Items must be empty before using Items Source
    【解决方案4】:

    如果您将 Items 与 ItemsSource 绑定,它似乎会被锁定...但是如果您使用 Item.Add(item) 添加每个项目,它就可以正常工作。 所以我最终做的是:

    var itemCollection = MapExtensions.GetChildren((Map)MyMap)
                                      .OfType<MapItemsControl>().FirstOrDefault();
    if(itemCollection != null && itemCollection.Items.Count >0)
    {
        itemCollection.Items.Clear();
    }
    foreach(var item in YourPushpinCollection)
    {
        itemCollection.Items.Add(item);
    }
    

    希望这会有所帮助:)

    【讨论】:

    • 这对我来说就像一个魅力!我现在看到的唯一问题是在删除和重新添加图钉时,它们在首次出现时往往会在视觉上“跳跃”几个像素。
    猜你喜欢
    • 2012-07-24
    • 2010-10-15
    • 1970-01-01
    • 2012-03-04
    • 2013-08-08
    • 2012-08-02
    • 2014-01-07
    • 1970-01-01
    • 2010-12-08
    相关资源
    最近更新 更多