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 = public static readonly StyledProperty<VerticalAlignment> VerticalContentAlignmentProperty =
AvaloniaProperty.Register<ContentControl, VerticalAlignment>(nameof(VerticalContentAlignment)); AvaloniaProperty.Register<ContentControl, VerticalAlignment>(nameof(VerticalContentAlignment));
static ContentControl()
{
ContentProperty.Changed.AddClassHandler<ContentControl>((x, e) => x.ContentChanged(e));
}
/// <summary> /// <summary>
/// Gets or sets the content to display. /// Gets or sets the content to display.
/// </summary> /// </summary>
@ -95,22 +90,20 @@ namespace Avalonia.Controls
/// <inheritdoc/> /// <inheritdoc/>
IAvaloniaList<ILogical> IContentPresenterHost.LogicalChildren => LogicalChildren; 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/> /// <inheritdoc/>
bool IContentPresenterHost.RegisterContentPresenter(ContentPresenter presenter) bool IContentPresenterHost.RegisterContentPresenter(ContentPresenter presenter)
{ {
return RegisterContentPresenter(presenter); return RegisterContentPresenter(presenter);
} }
protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
{
base.OnPropertyChanged(change);
if (change.Property == ContentProperty)
{
ContentChanged(change);
}
}
/// <summary> /// <summary>
/// Called when an <see cref="ContentPresenter"/> is registered with the control. /// Called when an <see cref="ContentPresenter"/> is registered with the control.
@ -129,11 +122,6 @@ namespace Avalonia.Controls
private void ContentChanged(AvaloniaPropertyChangedEventArgs e) private void ContentChanged(AvaloniaPropertyChangedEventArgs e)
{ {
if (BypassLogicalChildrenManagement)
{
return;
}
if (e.OldValue is ILogical oldChild) if (e.OldValue is ILogical oldChild)
{ {
LogicalChildren.Remove(oldChild); LogicalChildren.Remove(oldChild);

8
src/Avalonia.Controls/TransitioningContentControl.cs

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

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

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

Loading…
Cancel
Save