ObservableCollection移动抛出错误HRESULT E_FAIL已从对COM组件的调用返回

时间:2015-08-13 03:04:18

标签: c# xaml win-universal-app

我有一个绑定到ListView的ObservableCollection。 在ListView中,我在ItemTemplate中有两个按钮,向上移动并向下移动。 MoveUp按钮将使用ObservableCollection.Move将项目移动到ObservableCollection中的一个位置:

private void MoveUp(DataItem dataItem)
{
    var index = SubMissions.IndexOf(dataItem);
    if (index > 0)
    {
        DataItems.Move(index, index - 1);
    }
}

当我将一个项目从底部向上移动到顶部时,这可以正常工作。

但是,当我在最后一个项目上单击“上移”时,它将按预期向上移动。但是,当我单击“向上移动”然后成为最后一个项目时,它将失败并显示异常

 Error HRESULT E_FAIL has been returned from a call to a COM component.

堆栈跟踪:

at System.Collections.ObjectModel.ObservableCollection`1.OnCollectionChanged(NotifyCollectionChangedEventArgs e)
at System.Collections.ObjectModel.ObservableCollection`1.OnCollectionChanged(NotifyCollectionChangedAction action, Object item, Int32 index, Int32 oldIndex)
at System.Collections.ObjectModel.ObservableCollection`1.MoveItem(Int32 oldIndex, Int32 newIndex)
at System.Collections.ObjectModel.ObservableCollection`1.Move(Int32 oldIndex, Int32 newIndex)

而且,不仅是最后一个。如果我点击第二项上的向上,它可以正常工作。单击新的第二项上的向上,失败。 我已经确认索引的值是正确的。例如。我可以通过连续两次调用DataItems.Move(1,0)来重现错误。

我的XAML是:

<ListView x:Name="DataItemsListView" ItemsSource="{Binding DataItems}">
    <ListView.ItemTemplate>
        <DataTemplate>
            <StackPanel>
                <TextBlock Text="{Binding Title}"></TextBlock>
                <Button Content="MOVE UP" Command="{Binding ViewModel.MoveUpCommand, Source={StaticResource Locator}}" CommandParameter="{Binding}" />
                <Button Content="MOVE DOWN" Command="{Binding ViewModel.MoveDownCommand, Source={StaticResource Locator}}" CommandParameter="{Binding}" />

            </StackPanel>
         </DataTemplate>
     </ListView.ItemTemplate>
 </ListView>

Locator是一个使用SimpleIoc的静态对象。由MVVMLight.net提供。

/编辑:我发现当我跑步时.Move(1,0)在例如一种不同的方法,它工作正常。所以我必须改变命令中的集合吗? 但那我该怎么做呢?为什么它第一次起作用?

0 个答案:

没有答案