78 changed files with 537 additions and 602 deletions
@ -0,0 +1,24 @@ |
|||||
|
using System; |
||||
|
|
||||
|
namespace Avalonia.Controls.Templates |
||||
|
{ |
||||
|
public static class FuncTemplateNameScopeExtensions |
||||
|
{ |
||||
|
public static T RegisterInNameScope<T>(this T control, INameScope scope) |
||||
|
where T : StyledElement |
||||
|
{ |
||||
|
scope.Register(control.Name, control); |
||||
|
return control; |
||||
|
} |
||||
|
|
||||
|
public static T WithNameScope<T>(this T control, INameScope scope) |
||||
|
where T : StyledElement |
||||
|
{ |
||||
|
var existingScope = NameScope.GetNameScope(control); |
||||
|
if (existingScope != null && existingScope != scope) |
||||
|
throw new InvalidOperationException("Control already has a name scope"); |
||||
|
NameScope.SetNameScope(control, scope); |
||||
|
return control; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -1 +1 @@ |
|||||
Subproject commit 894b2c02827fd5eb16a338de5d5b6c9fbc60fef5 |
Subproject commit fc52295ea6c45f709bf63975026a4a18ce230be9 |
||||
@ -1,69 +0,0 @@ |
|||||
// 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 Avalonia.UnitTests; |
|
||||
using Xunit; |
|
||||
|
|
||||
namespace Avalonia.Controls.UnitTests |
|
||||
{ |
|
||||
public class StyledElementTests_NameScope |
|
||||
{ |
|
||||
[Fact] |
|
||||
public void Controls_Should_Register_With_NameScope() |
|
||||
{ |
|
||||
var root = new TestRoot |
|
||||
{ |
|
||||
Child = new Border |
|
||||
{ |
|
||||
Name = "foo", |
|
||||
Child = new Border |
|
||||
{ |
|
||||
Name = "bar", |
|
||||
} |
|
||||
} |
|
||||
}; |
|
||||
|
|
||||
root.ApplyTemplate(); |
|
||||
|
|
||||
Assert.Same(root.FindControl<Border>("foo"), root.Child); |
|
||||
Assert.Same(root.FindControl<Border>("bar"), ((Border)root.Child).Child); |
|
||||
} |
|
||||
|
|
||||
[Fact] |
|
||||
public void Control_Should_Unregister_With_NameScope() |
|
||||
{ |
|
||||
var root = new TestRoot |
|
||||
{ |
|
||||
Child = new Border |
|
||||
{ |
|
||||
Name = "foo", |
|
||||
Child = new Border |
|
||||
{ |
|
||||
Name = "bar", |
|
||||
} |
|
||||
} |
|
||||
}; |
|
||||
|
|
||||
root.Child = null; |
|
||||
|
|
||||
Assert.Null(root.FindControl<Border>("foo")); |
|
||||
Assert.Null(root.FindControl<Border>("bar")); |
|
||||
} |
|
||||
|
|
||||
[Fact] |
|
||||
public void Control_Should_Not_Register_With_Template_NameScope() |
|
||||
{ |
|
||||
var root = new TestTemplatedRoot |
|
||||
{ |
|
||||
Content = new Border |
|
||||
{ |
|
||||
Name = "foo", |
|
||||
} |
|
||||
}; |
|
||||
|
|
||||
root.ApplyTemplate(); |
|
||||
|
|
||||
Assert.Null(NameScope.GetNameScope((StyledElement)root.Presenter).Find("foo")); |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
Loading…
Reference in new issue