Browse Source

Remove BypassLogicalChildrenManagement and use OnPropertyChanged

pull/12173/head
daniel 3 years ago
parent
commit
953ec9b3ef
  1. 30
      src/Avalonia.Controls/ContentControl.cs
  2. 8
      src/Avalonia.Controls/TransitioningContentControl.cs
  3. 9
      tests/Avalonia.Controls.UnitTests/TransitioningContentControlTests.cs

30
src/Avalonia.Controls/ContentControl.cs

@ -40,11 +40,6 @@ namespace Avalonia.Controls
public static readonly StyledProperty<VerticalAlignment> VerticalContentAlignmentProperty =
AvaloniaProperty.Register<ContentControl, VerticalAlignment>(nameof(VerticalContentAlignment));
static ContentControl()
{
ContentProperty.Changed.AddClassHandler<ContentControl>((x, e) => x.ContentChanged(e));
}
/// <summary>
/// Gets or sets the content to display.
/// </summary>
@ -95,22 +90,20 @@ namespace Avalonia.Controls
/// <inheritdoc/>
IAvaloniaList<ILogical> IContentPresenterHost.LogicalChildren => LogicalChildren;
/// <summary>
/// Determine whether <see cref="ContentControl"/> manage his LogicalChildren
///(default behavior) himself, or leaves the management to the inherited control.
/// </summary>
/// <remarks>
/// The default value is false, So the <see cref="ContentControl"/> manages itself,
/// if you want to bypass this behavior and manage LogicalChildren yourself, set
/// the <see cref="BypassLogicalChildrenManagement"/> to true.
/// </remarks>
protected virtual bool BypassLogicalChildrenManagement => false;
/// <inheritdoc/>
bool IContentPresenterHost.RegisterContentPresenter(ContentPresenter presenter)
{
return RegisterContentPresenter(presenter);
}
protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
{
base.OnPropertyChanged(change);
if (change.Property == ContentProperty)
{
ContentChanged(change);
}
}
/// <summary>
/// Called when an <see cref="ContentPresenter"/> is registered with the control.
@ -129,11 +122,6 @@ namespace Avalonia.Controls
private void ContentChanged(AvaloniaPropertyChangedEventArgs e)
{
if (BypassLogicalChildrenManagement)
{
return;
}
if (e.OldValue is ILogical oldChild)
{
LogicalChildren.Remove(oldChild);

8
src/Avalonia.Controls/TransitioningContentControl.cs

@ -36,9 +36,6 @@ public class TransitioningContentControl : ContentControl
set => SetValue(PageTransitionProperty, value);
}
/// <inheritdoc/>
protected override bool BypassLogicalChildrenManagement => true;
protected override Size ArrangeOverride(Size finalSize)
{
var result = base.ArrangeOverride(finalSize);
@ -101,12 +98,13 @@ public class TransitioningContentControl : ContentControl
protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
{
base.OnPropertyChanged(change);
if (change.Property == ContentProperty)
{
UpdateContent(true);
return;
}
base.OnPropertyChanged(change);
}
private void UpdateContent(bool withTransition)

9
tests/Avalonia.Controls.UnitTests/TransitioningContentControlTests.cs

@ -184,7 +184,7 @@ namespace Avalonia.Controls.UnitTests
}
[Fact]
public void Logical_Children_Dont_Duplicated()
public void Logical_Children_Should_Not_Be_Duplicated()
{
using var app = Start();
var (target, transition) = CreateTarget("");
@ -194,18 +194,21 @@ namespace Avalonia.Controls.UnitTests
target.Content = childControl;
Assert.Equal(1, target.LogicalChildren.Count);
Assert.Equal(target.LogicalChildren[0], childControl);
}
[Fact]
public void First_Presenter_Register_TCC_As_Host()
public void First_Presenter_Should_Register_TCC_As_His_Host()
{
using var app = Start();
var (target, transition) = CreateTarget("");
target.PageTransition = null;
var childControl = new Control();
target.Presenter!.Content = childControl;
Assert.Contains(childControl, target.LogicalChildren);
Assert.Equal(1, target.LogicalChildren.Count);
Assert.Equal(target.LogicalChildren[0], childControl);
}
private static IDisposable Start()

Loading…
Cancel
Save