开发者

Deleting from Multiple Isolated Storages - windows phone 7

I currently have a itemsCollection and a historyCollection. Both are separate isolated .dat files.

The saving and displaying works fine for both of these isolated storages, however the problem is when I try to delete something out of the itemCollection I also want to remove all the items out of the historyItemCollection where the historyItemCollection contains that specific itemIndex.

Update:

The itemCollection could contain something like this:

  • a
  • b
  • c

The historyItemCollection could look like this:

  • a
  • b
  • c
  • b

So if I remove b (itemIndex 1) then I want both to be removed in the historyItemCollection.

I can remove the items out of the itemCollection fine, but the historyItemCollection throws errors.

        int i = 0;
        for (i = 0; i <= App.ViewModel.historyItemCollection.Count-1; i++)
        {
            if (App.ViewModel.historyItemCollection[i].VehicleId == (Application.Current as App).vehicleIndex)
            {
                App.ViewModel.historyItemCollection[i].Remove(i);
            }
        }

        App开发者_如何转开发.ViewModel.vehicleItemsCollection[(Application.Current as App).vehicleIndex].Remove((Application.Current as App).vehicleIndex);

The error message I'm getting: Parameter name: index

It fails on this line:

App.ViewModel.historyItemCollection[i].Remove(i);


In this line do you really mean both of these to be itemIndex?

App.ViewModel.historyItemCollection[(Application.Current as App).itemIndex].Remove((Application.Current as App).itemIndex);

Also, does calling RefreshItems() have any impact on itemIndex?

I'd recommend breaking your code up into more lines - then single stepping through.


As an aside, why are you setting the DataContext in this method? Then why are you also setting it twice - and to different types of objects each time. This seems unusual - perhaps you could instead set the DataContext globally to a new ViewModel class which has the history and items as children?


The problem is that you're removing items from a list as you iterate forwards through it.
As you delete items from the list you invalidate the count of total items in the list and your relative position.

If you must edit the list this way you should step backwards through it so as not to effect the indexing.

See also How to remove elements from a generic list while iterating over it?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜