From 208361b8faa597273d853d8661119fa0ee39043c Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Mon, 13 May 2019 17:18:51 +0200 Subject: [PATCH] Don't register controls with parent namescope. This reverts the changes in #843 because they were causing problems, as described by #2512. This should however not cause #829 to reappear because since #843 was merged we moved to Portable.Xaml and we're now registering controls with the root namescope in the XAML engine: https://github.com/AvaloniaUI/Avalonia/blob/b4577a1631755b391f3768e00264ac86c4300507/src/Markup/Avalonia.Markup.Xaml/PortableXaml/AvaloniaXamlObjectWriter.cs#L67 Fixes #2512 --- .../Primitives/TemplatedControl.cs | 2 +- src/Avalonia.Styling/StyledElement.cs | 17 -------------- .../Xaml/BasicTests.cs | 16 +++++++++++++ .../StyledElementTests.cs | 7 ++---- .../StyledElementTests_NameScope.cs | 23 ------------------- 5 files changed, 19 insertions(+), 46 deletions(-) diff --git a/src/Avalonia.Controls/Primitives/TemplatedControl.cs b/src/Avalonia.Controls/Primitives/TemplatedControl.cs index ba4c5027d0..32e220b789 100644 --- a/src/Avalonia.Controls/Primitives/TemplatedControl.cs +++ b/src/Avalonia.Controls/Primitives/TemplatedControl.cs @@ -357,7 +357,7 @@ namespace Avalonia.Controls.Primitives if (control.TemplatedParent == this) { - foreach (IControl child in control.GetVisualChildren()) + foreach (IControl child in control.GetLogicalChildren()) { RegisterNames(child, nameScope); } diff --git a/src/Avalonia.Styling/StyledElement.cs b/src/Avalonia.Styling/StyledElement.cs index d314a8d44e..ae2cec5561 100644 --- a/src/Avalonia.Styling/StyledElement.cs +++ b/src/Avalonia.Styling/StyledElement.cs @@ -677,23 +677,6 @@ namespace Avalonia if (Name != null) { _nameScope?.Register(Name, this); - - var visualParent = Parent as StyledElement; - - if (this is INameScope && visualParent != null) - { - // If we have e.g. a named UserControl in a window then we want that control - // to be findable by name from the Window, so register with both name scopes. - // This differs from WPF's behavior in that XAML manually registers controls - // with name scopes based on the XAML file in which the name attribute appears, - // but we're trying to avoid XAML magic in Avalonia in order to made code- - // created UIs easy. This will cause problems if a UserControl declares a name - // in its XAML and that control is included multiple times in a parent control - // (as the name will be duplicated), however at the moment I'm fine with saying - // "don't do that". - var parentNameScope = NameScope.FindNameScope(visualParent); - parentNameScope?.Register(Name, this); - } } } diff --git a/tests/Avalonia.Markup.Xaml.UnitTests/Xaml/BasicTests.cs b/tests/Avalonia.Markup.Xaml.UnitTests/Xaml/BasicTests.cs index 2e67541c1f..743fc82f29 100644 --- a/tests/Avalonia.Markup.Xaml.UnitTests/Xaml/BasicTests.cs +++ b/tests/Avalonia.Markup.Xaml.UnitTests/Xaml/BasicTests.cs @@ -197,6 +197,22 @@ namespace Avalonia.Markup.Xaml.UnitTests.Xaml Assert.Equal("Foo", button.Content); } + [Fact] + public void Named_UserControl_Is_Added_To_Parent_NameScope() + { + using (UnitTestApplication.Start(TestServices.StyledWindow)) + { + var xaml = @" + + +"; + + var control = AvaloniaXamlLoader.Parse(xaml); + + Assert.NotNull(control.FindControl("foo")); + } + } + [Fact] public void Direct_Content_In_ItemsControl_Is_Operational() { diff --git a/tests/Avalonia.Styling.UnitTests/StyledElementTests.cs b/tests/Avalonia.Styling.UnitTests/StyledElementTests.cs index 4970addd81..7fdd70799f 100644 --- a/tests/Avalonia.Styling.UnitTests/StyledElementTests.cs +++ b/tests/Avalonia.Styling.UnitTests/StyledElementTests.cs @@ -273,13 +273,10 @@ namespace Avalonia.Styling.UnitTests var root = new TestRoot(); var child = new Border(); - ((ISupportInitialize)child).BeginInit(); + child.BeginInit(); root.Child = child; child.Name = "foo"; - Assert.Null(root.FindControl("foo")); - ((ISupportInitialize)child).EndInit(); - - Assert.Same(root.FindControl("foo"), child); + child.EndInit(); } } diff --git a/tests/Avalonia.Styling.UnitTests/StyledElementTests_NameScope.cs b/tests/Avalonia.Styling.UnitTests/StyledElementTests_NameScope.cs index 47c540f44a..47c34dfd38 100644 --- a/tests/Avalonia.Styling.UnitTests/StyledElementTests_NameScope.cs +++ b/tests/Avalonia.Styling.UnitTests/StyledElementTests_NameScope.cs @@ -1,11 +1,6 @@ // Copyright (c) The Avalonia Project. All rights reserved. // Licensed under the MIT license. See licence.md file in the project root for full license information. -using System; -using Avalonia.Controls.Presenters; -using Avalonia.Controls.Templates; -using Avalonia.Rendering; -using Avalonia.Styling; using Avalonia.UnitTests; using Xunit; @@ -70,23 +65,5 @@ namespace Avalonia.Controls.UnitTests Assert.Null(NameScope.GetNameScope((StyledElement)root.Presenter).Find("foo")); } - - [Fact] - public void Control_That_Is_NameScope_Should_Register_With_Parent_NameScope() - { - UserControl userControl; - var root = new TestTemplatedRoot - { - Content = userControl = new UserControl - { - Name = "foo", - } - }; - - root.ApplyTemplate(); - - Assert.Same(userControl, root.FindControl("foo")); - Assert.Same(userControl, userControl.FindControl("foo")); - } } }