Skip to content

Commit

Permalink
avoid reading index property on args during reset. (#630)
Browse files Browse the repository at this point in the history
* avoid reading index property on args during reset.

* cr fix
  • Loading branch information
ranjeshj authored Apr 29, 2019
1 parent d0eb58a commit 2981c34
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
23 changes: 22 additions & 1 deletion dev/Repeater/APITests/InspectingDataSourceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using Windows.Foundation.Collections;
using Windows.UI.Xaml.Controls;
using Common;
using System;

#if USING_TAEF
using WEX.TestExecution;
Expand Down Expand Up @@ -242,7 +243,27 @@ IEnumerator IEnumerable.GetEnumerator()
private class WinRTVectorChangedEventArgs : IVectorChangedEventArgs
{
public CollectionChange CollectionChange { get; private set; }
public uint Index { get; private set; }

private uint _index;
public uint Index
{
get
{
if (CollectionChange == CollectionChange.Reset)
{
// C++/CX observable collection fails if accessing index
// when the args is for a Reset, so emulating that behavior here.
throw new InvalidOperationException();
}

return _index;
}

private set
{
_index = value;
}
}

public WinRTVectorChangedEventArgs(CollectionChange change, int index)
{
Expand Down
11 changes: 5 additions & 6 deletions dev/Repeater/InspectingDataSource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,6 @@ void InspectingDataSource::OnVectorChanged(
// Also note that we do not access the data - we just add nullptr. We just
// need the count.

const auto index = static_cast<int>(e.Index());
winrt::NotifyCollectionChangedAction action{};
int oldStartingIndex = -1;
int newStartingIndex = -1;
Expand All @@ -207,18 +206,18 @@ void InspectingDataSource::OnVectorChanged(
{
case winrt::Collections::CollectionChange::ItemInserted:
action = winrt::NotifyCollectionChangedAction::Add;
newStartingIndex = index;
newStartingIndex = static_cast<int>(e.Index());
newItems.Append(nullptr);
break;
case winrt::Collections::CollectionChange::ItemRemoved:
action = winrt::NotifyCollectionChangedAction::Remove;
oldStartingIndex = index;
oldStartingIndex = static_cast<int>(e.Index());
oldItems.Append(nullptr);
break;
case winrt::Collections::CollectionChange::ItemChanged:
action = winrt::NotifyCollectionChangedAction::Replace;
oldStartingIndex = index;
newStartingIndex = index;
oldStartingIndex = static_cast<int>(e.Index());
newStartingIndex = oldStartingIndex;
newItems.Append(nullptr);
oldItems.Append(nullptr);
break;
Expand All @@ -237,4 +236,4 @@ void InspectingDataSource::OnVectorChanged(
oldItems,
newStartingIndex,
oldStartingIndex));
}
}

0 comments on commit 2981c34

Please sign in to comment.