Skip to content
This repository was archived by the owner on May 1, 2024. It is now read-only.

[iOS] Prevent NRE in ListView OnItemSelected #883

Merged
merged 3 commits into from
Apr 25, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using Xamarin.Forms.CustomAttributes;
using Xamarin.Forms.Internals;
using System.Linq;

#if UITEST
using Xamarin.UITest;
using NUnit.Framework;
#endif

// Apply the default category of "Issues" to all of the tests in this assembly
// We use this as a catch-all for tests which haven't been individually categorized
#if UITEST
[assembly: NUnit.Framework.Category("Issues")]
#endif

namespace Xamarin.Forms.Controls.Issues
{
[Preserve(AllMembers = true)]
[Issue(IssueTracker.None, 0, "ListView crashes when disposed on ItemSelected", PlatformAffected.iOS)]
public class ListViewNRE : TestContentPage
{
const string Success = "Success";

protected override void Init()
{
var listView = new ListView
{
ItemsSource = Enumerable.Range(0, 10)
};

listView.ItemSelected += ListView_ItemSelected;

Content = listView;
}

void ListView_ItemSelected(object sender, SelectedItemChangedEventArgs e)
{
Content = new Label { Text = Success };
}

#if UITEST
[Test]
public void ListViewNRETest()
{
RunningApp.WaitForElement(q => q.Marked("1"));
RunningApp.Tap(q => q.Marked("1"));
RunningApp.WaitForElement(q => q.Marked(Success));
}
#endif
}
}
Original file line number Diff line number Diff line change
@@ -275,6 +275,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Bugzilla45874.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Unreported1.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Bugzilla53909.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ListViewNRE.cs" />
<Compile Include="$(MSBuildThisFileDirectory)_Template.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue1028.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue1075.cs" />
@@ -693,4 +694,4 @@
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
</ItemGroup>
</Project>
</Project>
3 changes: 3 additions & 0 deletions Xamarin.Forms.Platform.Android/Renderers/ListViewAdapter.cs
Original file line number Diff line number Diff line change
@@ -514,6 +514,9 @@ void Select(int index, AView view)

void SelectItem(object item)
{
if (_listView == null)
return;

int position = TemplatedItemsView.TemplatedItems.GetGlobalIndexOfItem(item);
AView view = null;
if (position != -1)
3 changes: 3 additions & 0 deletions Xamarin.Forms.Platform.iOS/Renderers/ListViewRenderer.cs
Original file line number Diff line number Diff line change
@@ -868,6 +868,9 @@ public void OnItemSelected(object sender, SelectedItemChangedEventArgs eventArg)
return;
}

if (List == null)
return;

var location = TemplatedItemsView.TemplatedItems.GetGroupAndIndexOfItem(eventArg.SelectedItem);
if (location.Item1 == -1 || location.Item2 == -1)
{