From c2f6a9c16c06b50c2795a6e57a6ae77b2d7291d1 Mon Sep 17 00:00:00 2001 From: Samantha Houts Date: Thu, 17 Aug 2017 08:59:13 -0700 Subject: [UWP] ListView ItemSelected event will fire only once on selection changed (#1005) * Add repro for 44886 * [UWP] Fire ListItemClicked when Selection changes This will automatically set the value on the renderer and prevent the double event from firing. * Clean up repro * Update test case for delection scenario * [Core] Allow ListView item deselection * [UWP] Send events when item is deselected, too * Test works better when you DO something. --- .../Bugzilla44886.cs | 90 ++++++++++++++++++++++ .../Xamarin.Forms.Controls.Issues.Shared.projitems | 1 + 2 files changed, 91 insertions(+) create mode 100644 Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla44886.cs (limited to 'Xamarin.Forms.Controls.Issues') diff --git a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla44886.cs b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla44886.cs new file mode 100644 index 00000000..b9150cd2 --- /dev/null +++ b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla44886.cs @@ -0,0 +1,90 @@ +using Xamarin.Forms.CustomAttributes; +using Xamarin.Forms.Internals; +using System.Collections.Generic; +using System.Runtime.CompilerServices; +using System.ComponentModel; + +#if UITEST +using Xamarin.UITest; +using NUnit.Framework; +#endif + +namespace Xamarin.Forms.Controls.Issues +{ + [Preserve(AllMembers = true)] + [Issue(IssueTracker.Bugzilla, 44886, "UWP Listview ItemSelected event triggered twice for each selection", PlatformAffected.UWP)] + public class Bugzilla44886 : TestContentPage + { + const string Item1 = "Item 1"; + const string Instructions = "Select one of the items in the list. The text in blue should show 1, indicating that the ItemSelected event fired once. If it shows 2, this test has failed. Be sure to also test Keyboard selection and Narrator selection. On UWP, the ItemSelected event should fire when an item is highlighted and again when it is un-highlighted (by pressing spacebar)."; + const string CountId = "countId"; + + Label _CountLabel = new Label { AutomationId = CountId, TextColor = Color.Blue }; + MyViewModel _vm = new MyViewModel(); + + [Preserve(AllMembers = true)] + class MyViewModel : INotifyPropertyChanged + { + int _count; + public int Count + { + get { return _count; } + set + { + if (value != _count) + { + _count = value; + RaisePropertyChanged(); + } + } + } + + void RaisePropertyChanged([CallerMemberName] string propertyName = null) + { + PropertyChangedEventHandler handler = PropertyChanged; + + handler?.Invoke(this, new PropertyChangedEventArgs(propertyName)); + } + + #region INotifyPropertyChanged implementation + + public event PropertyChangedEventHandler PropertyChanged; + + #endregion + } + + protected override void Init() + { + BindingContext = _vm; + + _CountLabel.SetBinding(Label.TextProperty, nameof(MyViewModel.Count)); + + var listView = new ListView + { + ItemsSource = new List { Item1, "Item 2", "Item 3", "Item 4", "Item 5" } + }; + listView.ItemSelected += ListView_ItemSelected; + + var stack = new StackLayout { Children = { new Label { Text = Instructions }, _CountLabel, listView } }; + Content = stack; + } + + void ListView_ItemSelected(object sender, SelectedItemChangedEventArgs e) + { + _vm.Count++; + } + +#if UITEST + [Test] + public void Bugzilla44886Test() + { + RunningApp.WaitForElement(q => q.Marked(Item1)); + RunningApp.Tap(q => q.Marked(Item1)); + + int count = int.Parse(RunningApp.Query(q => q.Marked(CountId))[0].Text); + + Assert.IsTrue(count == 1); + } +#endif + } +} \ No newline at end of file diff --git a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems index 672c8dfd..1274dd27 100644 --- a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems +++ b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems @@ -314,6 +314,7 @@ + -- cgit v1.2.3