[Core] Fixes setting to Null Binding Context in Picker (#3014)

- fixes #2499 
- fixes #2815 
- closes #2833
This commit is contained in:
Stephane Delcroix 2018-06-12 13:33:37 +02:00 коммит произвёл GitHub
Родитель 4bc56b4eff
Коммит f493397ea9
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
4 изменённых файлов: 63 добавлений и 10 удалений

Просмотреть файл

@ -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<string> { "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
}
}

Просмотреть файл

@ -241,6 +241,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Effects\AttachedStateEffectLabel.cs" /> <Compile Include="$(MSBuildThisFileDirectory)Effects\AttachedStateEffectLabel.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Effects\AttachedStateEffectList.cs" /> <Compile Include="$(MSBuildThisFileDirectory)Effects\AttachedStateEffectList.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue2767.cs" /> <Compile Include="$(MSBuildThisFileDirectory)Issue2767.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue2499.cs" />
<Compile Include="$(MSBuildThisFileDirectory)GitHub1878.cs" /> <Compile Include="$(MSBuildThisFileDirectory)GitHub1878.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Helpers\ISampleNativeControl.cs" /> <Compile Include="$(MSBuildThisFileDirectory)Helpers\ISampleNativeControl.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Helpers\ViewHelper.cs" /> <Compile Include="$(MSBuildThisFileDirectory)Helpers\ViewHelper.cs" />
@ -906,4 +907,4 @@
<Generator>MSBuild:UpdateDesignTimeXaml</Generator> <Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource> </EmbeddedResource>
</ItemGroup> </ItemGroup>
</Project> </Project>

Просмотреть файл

@ -23,7 +23,7 @@ namespace Xamarin.Forms.Core.UITests
if (Session == null) if (Session == null)
{ {
DesiredCapabilities appCapabilities = new DesiredCapabilities(); 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"); appCapabilities.SetCapability("deviceName", "WindowsPC");
Session = new WindowsDriver<WindowsElement>(new Uri(WindowsApplicationDriverUrl), appCapabilities); Session = new WindowsDriver<WindowsElement>(new Uri(WindowsApplicationDriverUrl), appCapabilities);
Assert.IsNotNull(Session); Assert.IsNotNull(Session);

Просмотреть файл

@ -150,8 +150,11 @@ namespace Xamarin.Forms
void OnItemsCollectionChanged(object sender, NotifyCollectionChangedEventArgs e) void OnItemsCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{ {
SelectedIndex = SelectedIndex.Clamp(-1, Items.Count - 1); var oldIndex = SelectedIndex;
UpdateSelectedItem(); 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) static void OnItemsSourceChanged(BindableObject bindable, object oldValue, object newValue)
@ -214,13 +217,13 @@ namespace Xamarin.Forms
((LockableObservableListWrapper)Items).InternalClear(); ((LockableObservableListWrapper)Items).InternalClear();
foreach (object item in ItemsSource) foreach (object item in ItemsSource)
((LockableObservableListWrapper)Items).InternalAdd(GetDisplayMember(item)); ((LockableObservableListWrapper)Items).InternalAdd(GetDisplayMember(item));
UpdateSelectedItem(); UpdateSelectedItem(SelectedIndex);
} }
static void OnSelectedIndexChanged(object bindable, object oldValue, object newValue) static void OnSelectedIndexChanged(object bindable, object oldValue, object newValue)
{ {
var picker = (Picker)bindable; var picker = (Picker)bindable;
picker.UpdateSelectedItem(); picker.UpdateSelectedItem(picker.SelectedIndex);
picker.SelectedIndexChanged?.Invoke(bindable, EventArgs.Empty); picker.SelectedIndexChanged?.Invoke(bindable, EventArgs.Empty);
} }
@ -239,19 +242,19 @@ namespace Xamarin.Forms
SelectedIndex = Items.IndexOf(selectedItem); SelectedIndex = Items.IndexOf(selectedItem);
} }
void UpdateSelectedItem() void UpdateSelectedItem(int index)
{ {
if (SelectedIndex == -1) { if (index == -1) {
SelectedItem = null; SelectedItem = null;
return; return;
} }
if (ItemsSource != null) { if (ItemsSource != null) {
SelectedItem = ItemsSource [SelectedIndex]; SelectedItem = ItemsSource [index];
return; return;
} }
SelectedItem = Items [SelectedIndex]; SelectedItem = Items [index];
} }
public IPlatformElementConfiguration<T, Picker> On<T>() where T : IConfigPlatform public IPlatformElementConfiguration<T, Picker> On<T>() where T : IConfigPlatform