-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6665 from AvaloniaUI/non-control-templates
Added support for non-control templates in XAML
- Loading branch information
Showing
8 changed files
with
117 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
namespace Avalonia.Controls.Templates | ||
{ | ||
public class TemplateResult<T> | ||
{ | ||
public T Result { get; } | ||
public INameScope NameScope { get; } | ||
|
||
public TemplateResult(T result, INameScope nameScope) | ||
{ | ||
Result = result; | ||
NameScope = nameScope; | ||
} | ||
|
||
public void Deconstruct(out T result, out INameScope scope) | ||
{ | ||
result = Result; | ||
scope = NameScope; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Submodule xamlil.github
updated
4 files
+10 −2 | src/XamlX/Ast/Clr.cs | |
+4 −3 | src/XamlX/IL/SreTypeSystem.cs | |
+24 −2 | src/XamlX/Transform/Transformers/DeferredContentTransformer.cs | |
+2 −0 | src/XamlX/Transform/XamlLanguageTypeMappings.cs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
tests/Avalonia.Markup.Xaml.UnitTests/Xaml/GenericTemplateTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
using System.Collections.Generic; | ||
using Avalonia.Controls; | ||
using Avalonia.Controls.Presenters; | ||
using Avalonia.Markup.Xaml.Templates; | ||
using Avalonia.Metadata; | ||
using Avalonia.UnitTests; | ||
using Xunit; | ||
|
||
namespace Avalonia.Markup.Xaml.UnitTests.Xaml | ||
{ | ||
public class SampleTemplatedObject : StyledElement | ||
{ | ||
[Content] public List<SampleTemplatedObject> Content { get; set; } = new List<SampleTemplatedObject>(); | ||
public string Foo { get; set; } | ||
} | ||
|
||
public class SampleTemplatedObjectTemplate | ||
{ | ||
[Content] | ||
[TemplateContent(TemplateResultType = typeof(SampleTemplatedObject))] | ||
public object Content { get; set; } | ||
} | ||
|
||
public class SampleTemplatedObjectContainer | ||
{ | ||
public SampleTemplatedObjectTemplate Template { get; set; } | ||
} | ||
|
||
public class GenericTemplateTests | ||
{ | ||
[Fact] | ||
public void DataTemplate_Can_Be_Empty() | ||
{ | ||
using (UnitTestApplication.Start(TestServices.StyledWindow)) | ||
{ | ||
var xaml = @" | ||
<s:SampleTemplatedObjectContainer xmlns='https://github.com/avaloniaui' | ||
xmlns:sys='clr-namespace:System;assembly=netstandard' | ||
xmlns:s='clr-namespace:Avalonia.Markup.Xaml.UnitTests.Xaml' | ||
xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'> | ||
<s:SampleTemplatedObjectContainer.Template> | ||
<s:SampleTemplatedObjectTemplate> | ||
<s:SampleTemplatedObject x:Name='root'> | ||
<s:SampleTemplatedObject x:Name='child1' Foo='foo' /> | ||
<s:SampleTemplatedObject x:Name='child2' Foo='bar' /> | ||
</s:SampleTemplatedObject> | ||
</s:SampleTemplatedObjectTemplate> | ||
</s:SampleTemplatedObjectContainer.Template> | ||
</s:SampleTemplatedObjectContainer>"; | ||
var container = | ||
(SampleTemplatedObjectContainer)AvaloniaRuntimeXamlLoader.Load(xaml, | ||
typeof(GenericTemplateTests).Assembly); | ||
var res = TemplateContent.Load<SampleTemplatedObject>(container.Template.Content); | ||
Assert.Equal(res.Result, res.NameScope.Find("root")); | ||
Assert.Equal(res.Result.Content[0], res.NameScope.Find("child1")); | ||
Assert.Equal(res.Result.Content[1], res.NameScope.Find("child2")); | ||
Assert.Equal("foo", res.Result.Content[0].Foo); | ||
Assert.Equal("bar", res.Result.Content[1].Foo); | ||
} | ||
} | ||
} | ||
} |