[Shell] propagate BindingContext to SearchHandler (#5730)

- fixes #5705
This commit is contained in:
Stephane Delcroix 2019-04-03 08:49:01 +02:00 коммит произвёл GitHub
Родитель abd3f350de
Коммит 3f65849d76
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
6 изменённых файлов: 64 добавлений и 4 удалений

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

@ -199,6 +199,9 @@ namespace Xamarin.Forms
BindingContextChanged?.Invoke(this, EventArgs.Empty);
if (Shell.GetBackButtonBehavior(this) is BackButtonBehavior buttonBehavior)
SetInheritedBindingContext(buttonBehavior, BindingContext);
if (Shell.GetSearchHandler(this) is SearchHandler searchHandler)
SetInheritedBindingContext(searchHandler, BindingContext);
}
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)

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

@ -35,7 +35,16 @@ namespace Xamarin.Forms
BindableProperty.CreateAttached("NavBarIsVisible", typeof(bool), typeof(Shell), true);
public static readonly BindableProperty SearchHandlerProperty =
BindableProperty.CreateAttached("SearchHandler", typeof(SearchHandler), typeof(Shell), null, BindingMode.OneTime);
BindableProperty.CreateAttached("SearchHandler", typeof(SearchHandler), typeof(Shell), null, BindingMode.OneTime,
propertyChanged: OnSearchHandlerPropertyChanged);
static void OnSearchHandlerPropertyChanged(BindableObject bindable, object oldValue, object newValue)
{
if (oldValue is SearchHandler oldHandler)
SetInheritedBindingContext(oldHandler, null);
if (newValue is SearchHandler newHandler)
SetInheritedBindingContext(newHandler, bindable.BindingContext);
}
public static readonly BindableProperty SetPaddingInsetsProperty =
BindableProperty.CreateAttached("SetPaddingInsets", typeof(bool), typeof(Shell), false);
@ -1069,14 +1078,11 @@ namespace Xamarin.Forms
return element;
}
#region IPropertyPropagationController
void IPropertyPropagationController.PropagatePropertyChanged(string propertyName)
{
PropertyPropagationExtensions.PropagatePropertyChanged(propertyName, this, LogicalChildren);
if (FlyoutHeaderView != null)
PropertyPropagationExtensions.PropagatePropertyChanged(propertyName, this, new[] { FlyoutHeaderView });
}
#endregion
}
}

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

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Shell xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="Xamarin.Forms.Xaml.UnitTests.Gh5705">
<Shell.SearchHandler>
<SearchHandler x:Name="searchHandler"/>
</Shell.SearchHandler>
</Shell>

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

@ -0,0 +1,45 @@
using System;
using System.Collections.Generic;
using NUnit.Framework;
using Xamarin.Forms;
using Xamarin.Forms.Core.UnitTests;
namespace Xamarin.Forms.Xaml.UnitTests
{
public partial class Gh5705 : Shell
{
public Gh5705() => InitializeComponent();
public Gh5705(bool useCompiledXaml)
{
//this stub will be replaced at compile time
}
[TestFixture] class Tests
{
IReadOnlyList<string> _flags;
[SetUp]
public void Setup()
{
Device.PlatformServices = new MockPlatformServices();
_flags = Device.Flags;
Device.SetFlags(new List<string>() { ExperimentalFlags.ShellExperimental});
}
[TearDown]
public void TearDown()
{
Device.PlatformServices = null;
Device.SetFlags(_flags);
}
[Test]
public void SearchHandlerIneritBC([Values(false, true)]bool useCompiledXaml)
{
var vm = new object();
var shell = new Gh5705(useCompiledXaml) { BindingContext = vm };
Assert.That(shell.searchHandler.BindingContext, Is.EqualTo(vm));
}
}
}
}