Browse Source

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: b4577a1631/src/Markup/Avalonia.Markup.Xaml/PortableXaml/AvaloniaXamlObjectWriter.cs (L67)

Fixes #2512
pull/2515/head
Steven Kirk 7 years ago
parent
commit
208361b8fa
  1. 2
      src/Avalonia.Controls/Primitives/TemplatedControl.cs
  2. 17
      src/Avalonia.Styling/StyledElement.cs
  3. 16
      tests/Avalonia.Markup.Xaml.UnitTests/Xaml/BasicTests.cs
  4. 7
      tests/Avalonia.Styling.UnitTests/StyledElementTests.cs
  5. 23
      tests/Avalonia.Styling.UnitTests/StyledElementTests_NameScope.cs

2
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);
}

17
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);
}
}
}

16
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 = @"
<Window xmlns='https://github.com/avaloniaui'>
<UserControl Name='foo'/>
</Window>";
var control = AvaloniaXamlLoader.Parse<Window>(xaml);
Assert.NotNull(control.FindControl<UserControl>("foo"));
}
}
[Fact]
public void Direct_Content_In_ItemsControl_Is_Operational()
{

7
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<Border>("foo"));
((ISupportInitialize)child).EndInit();
Assert.Same(root.FindControl<Border>("foo"), child);
child.EndInit();
}
}

23
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<UserControl>("foo"));
Assert.Same(userControl, userControl.FindControl<UserControl>("foo"));
}
}
}

Loading…
Cancel
Save