[C] Allow recursive parts of binding path (#4028)

- fixes #3994
This commit is contained in:
Stephane Delcroix 2018-11-21 08:33:15 +01:00 коммит произвёл GitHub
Родитель 17f5ce556b
Коммит b2d7a4e1c1
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
2 изменённых файлов: 25 добавлений и 14 удалений

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

@ -2243,5 +2243,21 @@ namespace Xamarin.Forms.Core.UnitTests
bindable.BindingContext = new MockViewModel();
Assert.That(bindable.GetValue(property), Is.EqualTo("fallback"));
}
[Test]
//https://github.com/xamarin/Xamarin.Forms/issues/3994
public void INPCOnBindingWithSource()
{
var page = new ContentPage {Title = "Foo"};
page.BindingContext = page;
var label = new Label();
page.Content = label;
label.SetBinding(Label.TextProperty, new Binding("BindingContext.Title", source:page));
Assert.That(label.Text, Is.EqualTo("Foo"));
page.Title = "Bar";
Assert.That(label.Text, Is.EqualTo("Bar"));
}
}
}

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

@ -124,8 +124,7 @@ namespace Xamarin.Forms
if (!part.IsSelf && current != null)
{
// Allow the object instance itself to provide its own TypeInfo
var reflectable = current as IReflectableType;
TypeInfo currentType = reflectable != null ? reflectable.GetTypeInfo() : current.GetType().GetTypeInfo();
TypeInfo currentType = current is IReflectableType reflectable ? reflectable.GetTypeInfo() : current.GetType().GetTypeInfo();
if (part.LastGetter == null || !part.LastGetter.DeclaringType.GetTypeInfo().IsAssignableFrom(currentType))
SetupPart(currentType, part);
@ -133,21 +132,17 @@ namespace Xamarin.Forms
part.TryGetValue(current, out current);
}
if (!part.IsSelf && current != null)
{
if ((needsGetter && part.LastGetter == null) || (needsSetter && part.NextPart == null && part.LastSetter == null))
{
Log.Warning("Binding", PropertyNotFoundErrorMessage, part.Content, current, target.GetType(), property.PropertyName);
break;
}
if ( !part.IsSelf
&& current != null
&& ( (needsGetter && part.LastGetter == null)
|| (needsSetter && part.NextPart == null && part.LastSetter == null))) {
Log.Warning("Binding", PropertyNotFoundErrorMessage, part.Content, current, target.GetType(), property.PropertyName);
break;
}
if (mode == BindingMode.OneWay || mode == BindingMode.TwoWay)
{
var inpc = current as INotifyPropertyChanged;
if (inpc != null && !ReferenceEquals(current, previous))
if (part.NextPart != null && (mode == BindingMode.OneWay || mode == BindingMode.TwoWay)
&& current is INotifyPropertyChanged inpc)
part.Subscribe(inpc);
}
previous = current;
}