Browse Source

Don't raise ResourceChanged on reparenting...

...when the new parent isn't attached to the tree. This prevents us sending a load of useless `ResourcesChanged` events.
pull/3957/head
Steven Kirk 6 years ago
parent
commit
dcdaf5e0bf
  1. 10
      src/Avalonia.Styling/StyledElement.cs
  2. 2
      tests/Avalonia.Controls.UnitTests/Primitives/TemplatedControlTests.cs
  3. 35
      tests/Avalonia.Markup.Xaml.UnitTests/MarkupExtensions/DynamicResourceExtensionTests.cs
  4. 4
      tests/Avalonia.Styling.UnitTests/StyledElementTests.cs

10
src/Avalonia.Styling/StyledElement.cs

@ -418,12 +418,16 @@ namespace Avalonia
var e = new LogicalTreeAttachmentEventArgs(newRoot, this, parent);
OnAttachedToLogicalTreeCore(e);
}
else
else if (parent is null)
{
// If we were attached to the logical tree, we piggyback on the tree traversal
// there to raise resources changed notifications. If we're being removed from
// the logical tree or attached to a control which is not rooted, then traverse
// the tree raising notifications now.
// the logical tree, then traverse the tree raising notifications now.
//
// We don't raise resources changed notifications if we're being attached to a
// non-rooted control beacuse it's unlikely that dynamic resources need to be
// correct until the control is added to the tree, and it causes a *lot* of
// notifications.
NotifyResourcesChanged();
}

2
tests/Avalonia.Controls.UnitTests/Primitives/TemplatedControlTests.cs

@ -536,6 +536,7 @@ namespace Avalonia.Controls.UnitTests.Primitives
}),
};
var root = new TestRoot(target);
target.ApplyTemplate();
var contentPresenter = Assert.IsType<ContentPresenter>(target.GetVisualChildren().Single());
@ -565,6 +566,7 @@ namespace Avalonia.Controls.UnitTests.Primitives
}),
};
var root = new TestRoot(target);
target.ApplyTemplate();
var contentPresenter = Assert.IsType<ContentPresenter>(target.GetVisualChildren().Single());

35
tests/Avalonia.Markup.Xaml.UnitTests/MarkupExtensions/DynamicResourceExtensionTests.cs

@ -634,33 +634,36 @@ namespace Avalonia.Markup.Xaml.UnitTests.MarkupExtensions
[Fact]
public void Control_Property_Is_Updated_When_Parent_Is_Changed()
{
var xaml = @"
<UserControl xmlns='https://github.com/avaloniaui'
using (StyledWindow())
{
var xaml = @"
<Window xmlns='https://github.com/avaloniaui'
xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
<UserControl.Resources>
<Window.Resources>
<SolidColorBrush x:Key='brush'>#ff506070</SolidColorBrush>
</UserControl.Resources>
</Window.Resources>
<Border Name='border' Background='{DynamicResource brush}'/>
</UserControl>";
</Window>";
var loader = new AvaloniaXamlLoader();
var userControl = (UserControl)loader.Load(xaml);
var border = userControl.FindControl<Border>("border");
var loader = new AvaloniaXamlLoader();
var window = (Window)loader.Load(xaml);
var border = window.FindControl<Border>("border");
DelayedBinding.ApplyBindings(border);
DelayedBinding.ApplyBindings(border);
var brush = (SolidColorBrush)border.Background;
Assert.Equal(0xff506070, brush.Color.ToUint32());
var brush = (SolidColorBrush)border.Background;
Assert.Equal(0xff506070, brush.Color.ToUint32());
userControl.Content = null;
window.Content = null;
Assert.Null(border.Background);
Assert.Null(border.Background);
userControl.Content = border;
window.Content = border;
brush = (SolidColorBrush)border.Background;
Assert.Equal(0xff506070, brush.Color.ToUint32());
brush = (SolidColorBrush)border.Background;
Assert.Equal(0xff506070, brush.Color.ToUint32());
}
}
[Fact]

4
tests/Avalonia.Styling.UnitTests/StyledElementTests.cs

@ -530,9 +530,9 @@ namespace Avalonia.Styling.UnitTests
}
[Fact]
public void Changing_Parent_Raises_ResourcesChanged()
public void Adding_To_Logical_Tree_Raises_ResourcesChanged()
{
var target = new TestControl();
var target = new TestRoot();
var parent = new Decorator { Resources = { { "foo", "bar" } } };
var raised = 0;

Loading…
Cancel
Save