From 74bbdc193b97cf597c1104c892fbb30b7103b24e Mon Sep 17 00:00:00 2001 From: Andrey Kunchev Date: Fri, 13 Nov 2020 18:21:58 +0200 Subject: [PATCH] actually compile cast in compiled binding --- .../XamlIlBindingPathHelper.cs | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/Markup/Avalonia.Markup.Xaml.Loader/CompilerExtensions/XamlIlBindingPathHelper.cs b/src/Markup/Avalonia.Markup.Xaml.Loader/CompilerExtensions/XamlIlBindingPathHelper.cs index 03ec32b9cf..414ecc760a 100644 --- a/src/Markup/Avalonia.Markup.Xaml.Loader/CompilerExtensions/XamlIlBindingPathHelper.cs +++ b/src/Markup/Avalonia.Markup.Xaml.Loader/CompilerExtensions/XamlIlBindingPathHelper.cs @@ -242,6 +242,16 @@ namespace Avalonia.Markup.Xaml.XamlIl.CompilerExtensions case RawSourceBindingExpressionNode rawSource: nodes.Add(new RawSourcePathElementNode(rawSource.RawSource)); break; + case BindingExpressionGrammar.TypeCastNode typeCastNode: + var castType = GetType(typeCastNode.Namespace, typeCastNode.TypeName); + + if (castType is null) + { + throw new XamlX.XamlParseException($"Unable to resolve cast to type {typeCastNode.Namespace}:{typeCastNode.TypeName} based on XAML tree.", lineInfo); + } + + nodes.Add(new TypeCastPathElementNode(castType)); + break; } } @@ -625,6 +635,21 @@ namespace Avalonia.Markup.Xaml.XamlIl.CompilerExtensions } } + class TypeCastPathElementNode : IXamlIlBindingPathElementNode + { + public TypeCastPathElementNode(IXamlType ancestorType) + { + Type = ancestorType; + } + + public IXamlType Type { get; } + + public void Emit(XamlIlEmitContext context, IXamlILEmitter codeGen) + { + codeGen.EmitCall(context.GetAvaloniaTypes().CompiledBindingPathBuilder.FindMethod(m => m.Name == "TypeCast").MakeGenericMethod(new[] { Type })); + } + } + class XamlIlBindingPathNode : XamlAstNode, IXamlIlBindingPathNode, IXamlAstEmitableNode { private readonly List _transformElements;