committed by
GitHub
4 changed files with 106 additions and 6 deletions
@ -0,0 +1,34 @@ |
|||||
|
using System.Linq; |
||||
|
using XamlX; |
||||
|
using XamlX.Ast; |
||||
|
using XamlX.Transform; |
||||
|
using XamlX.Transform.Transformers; |
||||
|
|
||||
|
namespace Avalonia.Markup.Xaml.XamlIl.CompilerExtensions.Transformers; |
||||
|
|
||||
|
internal class AvaloniaXamlIlSetterTargetTypeMetadataTransformer : IXamlAstTransformer |
||||
|
{ |
||||
|
public IXamlAstNode Transform(AstTransformationContext context, IXamlAstNode node) |
||||
|
{ |
||||
|
if (node is XamlAstObjectNode on |
||||
|
&& on.Children.FirstOrDefault(c => c is XamlAstXmlDirective |
||||
|
{ |
||||
|
Namespace: XamlNamespaces.Xaml2006, |
||||
|
Name: "SetterTargetType" |
||||
|
}) is { } typeDirective) |
||||
|
{ |
||||
|
var value = ((XamlAstXmlDirective)typeDirective).Values.Single(); |
||||
|
var type = value is XamlTypeExtensionNode typeNode ? typeNode.Value |
||||
|
: value is XamlAstTextNode tn ? TypeReferenceResolver.ResolveType(context, tn.Text, false, tn, true) |
||||
|
: null; |
||||
|
on.Children.Remove(typeDirective); |
||||
|
|
||||
|
if (type is null) |
||||
|
{ |
||||
|
throw new XamlParseException("Unable to resolve SetterTargetType type", typeDirective); |
||||
|
} |
||||
|
return new AvaloniaXamlIlTargetTypeMetadataNode(on, type, AvaloniaXamlIlTargetTypeMetadataNode.ScopeTypes.Style); |
||||
|
} |
||||
|
return node; |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,51 @@ |
|||||
|
using Avalonia.Controls; |
||||
|
using Avalonia.Styling; |
||||
|
using Avalonia.UnitTests; |
||||
|
using Xunit; |
||||
|
|
||||
|
namespace Avalonia.Markup.Xaml.UnitTests; |
||||
|
|
||||
|
public class SetterTests : XamlTestBase |
||||
|
{ |
||||
|
[Fact] |
||||
|
public void SetterTargetType_Should_Understand_xType_Extensions() |
||||
|
{ |
||||
|
using (UnitTestApplication.Start(TestServices.StyledWindow)) |
||||
|
{ |
||||
|
var xaml = @"
|
||||
|
<Animation xmlns='https://github.com/avaloniaui' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' x:SetterTargetType='{x:Type ContentControl}'>
|
||||
|
<KeyFrame> |
||||
|
<Setter Property='Content' Value='{Binding}'/> |
||||
|
</KeyFrame> |
||||
|
<KeyFrame> |
||||
|
<Setter Property='Content' Value='{Binding}'/> |
||||
|
</KeyFrame> |
||||
|
</Animation>";
|
||||
|
var animation = (Animation.Animation)AvaloniaRuntimeXamlLoader.Load(xaml); |
||||
|
var setter = (Setter)animation.Children[0].Setters[0]; |
||||
|
|
||||
|
Assert.Equal(typeof(ContentControl), setter.Property.OwnerType); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void SetterTargetType_Should_Understand_Type_From_Xmlns() |
||||
|
{ |
||||
|
using (UnitTestApplication.Start(TestServices.StyledWindow)) |
||||
|
{ |
||||
|
var xaml = @"
|
||||
|
<av:Animation xmlns:av='https://github.com/avaloniaui' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' x:SetterTargetType='av:ContentControl'>
|
||||
|
<av:KeyFrame> |
||||
|
<av:Setter Property='Content' Value='{av:Binding}'/> |
||||
|
</av:KeyFrame> |
||||
|
<av:KeyFrame> |
||||
|
<av:Setter Property='Content' Value='{av:Binding}'/> |
||||
|
</av:KeyFrame> |
||||
|
</av:Animation>";
|
||||
|
var animation = (Animation.Animation)AvaloniaRuntimeXamlLoader.Load(xaml); |
||||
|
var setter = (Setter)animation.Children[0].Setters[0]; |
||||
|
|
||||
|
Assert.Equal(typeof(ContentControl), setter.Property.OwnerType); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue