diff --git a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue2499.cs b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue2499.cs new file mode 100644 index 000000000..532ccf370 --- /dev/null +++ b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue2499.cs @@ -0,0 +1,49 @@ +using System.Collections.Generic; +using Xamarin.Forms.CustomAttributes; +using Xamarin.Forms.Internals; + +#if UITEST +using Xamarin.UITest; +using Xamarin.UITest.Queries; +using NUnit.Framework; +#endif + +namespace Xamarin.Forms.Controls.Issues +{ + [Preserve(AllMembers = true)] + [Issue(IssueTracker.Github, 2499, "Binding Context set to Null in Picker", PlatformAffected.All)] + public class Issue2499 : TestContentPage + { + protected override void Init() + { + var _picker = new Picker() + { + ItemsSource = new List { "cat", "mouse", "rabbit" }, + AutomationId = "picker", + }; + _picker.SelectedIndexChanged += (_, __) => _picker.ItemsSource = null; + + Content = new StackLayout() + { + Children = + { + _picker + } + }; + } + +#if UITEST + [Test] + public void Issue2499Test () + { + RunningApp.Tap ("picker"); + AppResult[] items = RunningApp.Query("cat"); + Assert.AreNotEqual(items.Length, 0); + + RunningApp.Tap ("cat"); + items = RunningApp.Query("cat"); + Assert.AreEqual(items.Length, 0); + } +#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 66bce43e8..8147b62dd 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 @@ -241,6 +241,7 @@ + @@ -906,4 +907,4 @@ MSBuild:UpdateDesignTimeXaml - \ No newline at end of file + diff --git a/Xamarin.Forms.Core.Windows.UITests/WindowsTestBase.cs b/Xamarin.Forms.Core.Windows.UITests/WindowsTestBase.cs index 72d4cbafe..38f8947b2 100644 --- a/Xamarin.Forms.Core.Windows.UITests/WindowsTestBase.cs +++ b/Xamarin.Forms.Core.Windows.UITests/WindowsTestBase.cs @@ -23,7 +23,7 @@ namespace Xamarin.Forms.Core.UITests if (Session == null) { DesiredCapabilities appCapabilities = new DesiredCapabilities(); - appCapabilities.SetCapability("app", "0d4424f6-1e29-4476-ac00-ba22c3789cb6_wzjw7qdpbr1br!App"); + appCapabilities.SetCapability("app", "0d4424f6-1e29-4476-ac00-ba22c3789cb6_ph1m9x8skttmg!App"); appCapabilities.SetCapability("deviceName", "WindowsPC"); Session = new WindowsDriver(new Uri(WindowsApplicationDriverUrl), appCapabilities); Assert.IsNotNull(Session); diff --git a/Xamarin.Forms.Core/Picker.cs b/Xamarin.Forms.Core/Picker.cs index 865515471..b58a7ddf8 100644 --- a/Xamarin.Forms.Core/Picker.cs +++ b/Xamarin.Forms.Core/Picker.cs @@ -150,8 +150,11 @@ namespace Xamarin.Forms void OnItemsCollectionChanged(object sender, NotifyCollectionChangedEventArgs e) { - SelectedIndex = SelectedIndex.Clamp(-1, Items.Count - 1); - UpdateSelectedItem(); + var oldIndex = SelectedIndex; + var newIndex = SelectedIndex = SelectedIndex.Clamp(-1, Items.Count - 1); + // If the index has not changed, still need to change the selected item + if (newIndex == oldIndex) + UpdateSelectedItem(newIndex); } static void OnItemsSourceChanged(BindableObject bindable, object oldValue, object newValue) @@ -214,13 +217,13 @@ namespace Xamarin.Forms ((LockableObservableListWrapper)Items).InternalClear(); foreach (object item in ItemsSource) ((LockableObservableListWrapper)Items).InternalAdd(GetDisplayMember(item)); - UpdateSelectedItem(); + UpdateSelectedItem(SelectedIndex); } static void OnSelectedIndexChanged(object bindable, object oldValue, object newValue) { var picker = (Picker)bindable; - picker.UpdateSelectedItem(); + picker.UpdateSelectedItem(picker.SelectedIndex); picker.SelectedIndexChanged?.Invoke(bindable, EventArgs.Empty); } @@ -239,19 +242,19 @@ namespace Xamarin.Forms SelectedIndex = Items.IndexOf(selectedItem); } - void UpdateSelectedItem() + void UpdateSelectedItem(int index) { - if (SelectedIndex == -1) { + if (index == -1) { SelectedItem = null; return; } if (ItemsSource != null) { - SelectedItem = ItemsSource [SelectedIndex]; + SelectedItem = ItemsSource [index]; return; } - SelectedItem = Items [SelectedIndex]; + SelectedItem = Items [index]; } public IPlatformElementConfiguration On() where T : IConfigPlatform