From 7d8dcc02ecbd389726f6c82302121741fc3bb392 Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Thu, 10 May 2018 00:53:10 +0200 Subject: [PATCH 1/3] Added failing test for #1568 --- .../AvaloniaObjectTests_Attached.cs | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/tests/Avalonia.Base.UnitTests/AvaloniaObjectTests_Attached.cs b/tests/Avalonia.Base.UnitTests/AvaloniaObjectTests_Attached.cs index ab2a2d899d..2262f4cfa2 100644 --- a/tests/Avalonia.Base.UnitTests/AvaloniaObjectTests_Attached.cs +++ b/tests/Avalonia.Base.UnitTests/AvaloniaObjectTests_Attached.cs @@ -24,10 +24,23 @@ namespace Avalonia.Base.UnitTests Assert.Throws(() => target.SetValue(Class2.FooProperty, "throw")); } + [Fact] + public void AvaloniaProperty_Initialized_Is_Called_For_Attached_Property() + { + bool raised = false; + + using (Class1.FooProperty.Initialized.Subscribe(x => raised = true)) + { + new Class3(); + } + + Assert.True(raised); + } + private class Class1 : AvaloniaObject { public static readonly AttachedProperty FooProperty = - AvaloniaProperty.RegisterAttached( + AvaloniaProperty.RegisterAttached( "Foo", "foodefault", validate: ValidateFoo); @@ -48,5 +61,9 @@ namespace Avalonia.Base.UnitTests public static readonly AttachedProperty FooProperty = Class1.FooProperty.AddOwner(); } + + private class Class3 : AvaloniaObject + { + } } } From e4df163ef0fff967d3483642aa49fa4059554a50 Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Thu, 10 May 2018 01:09:38 +0200 Subject: [PATCH 2/3] Call Initialized for attached properties. #1499 broke `AvaloniaProperty.Initialized` for attached properties - they were no longer called. Fixes #1568. --- src/Avalonia.Base/AvaloniaObject.cs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/Avalonia.Base/AvaloniaObject.cs b/src/Avalonia.Base/AvaloniaObject.cs index 4ab813333d..68b9871fd1 100644 --- a/src/Avalonia.Base/AvaloniaObject.cs +++ b/src/Avalonia.Base/AvaloniaObject.cs @@ -71,7 +71,8 @@ namespace Avalonia public AvaloniaObject() { VerifyAccess(); - foreach (var property in AvaloniaPropertyRegistry.Instance.GetRegistered(this)) + + void Notify(AvaloniaProperty property) { object value = property.IsDirect ? ((IDirectPropertyAccessor)property).GetValue(this) : @@ -86,6 +87,16 @@ namespace Avalonia property.NotifyInitialized(e); } + + foreach (var property in AvaloniaPropertyRegistry.Instance.GetRegistered(this)) + { + Notify(property); + } + + foreach (var property in AvaloniaPropertyRegistry.Instance.GetRegisteredAttached(this.GetType())) + { + Notify(property); + } } /// From 44fe6aa6acdbeb0fea761b2a136046356ce06a82 Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Thu, 10 May 2018 01:31:28 +0200 Subject: [PATCH 3/3] Fix failing unit tests. AvaloniaProperty registrations are for life (of the test runner) not for christmas. --- .../AvaloniaObjectTests_Attached.cs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/tests/Avalonia.Base.UnitTests/AvaloniaObjectTests_Attached.cs b/tests/Avalonia.Base.UnitTests/AvaloniaObjectTests_Attached.cs index 2262f4cfa2..02600f5e00 100644 --- a/tests/Avalonia.Base.UnitTests/AvaloniaObjectTests_Attached.cs +++ b/tests/Avalonia.Base.UnitTests/AvaloniaObjectTests_Attached.cs @@ -37,10 +37,14 @@ namespace Avalonia.Base.UnitTests Assert.True(raised); } - private class Class1 : AvaloniaObject + private class Base : AvaloniaObject + { + } + + private class Class1 : Base { public static readonly AttachedProperty FooProperty = - AvaloniaProperty.RegisterAttached( + AvaloniaProperty.RegisterAttached( "Foo", "foodefault", validate: ValidateFoo); @@ -56,13 +60,13 @@ namespace Avalonia.Base.UnitTests } } - private class Class2 : AvaloniaObject + private class Class2 : Base { public static readonly AttachedProperty FooProperty = Class1.FooProperty.AddOwner(); } - private class Class3 : AvaloniaObject + private class Class3 : Base { } }