예제 #1
0
    public ResolvedView ResolveView(string xaml)
    {
        _resolvedClass = null;
        _xaml          = XDocumentXamlParser.Parse(xaml, new Dictionary <string, string>
        {
            { XamlNamespaces.Blend2008, XamlNamespaces.Blend2008 }
        });

        _compiler.Transform(_xaml);
        _xaml.Root.Visit(this);
        _xaml.Root.VisitChildren(this);
        return(_resolvedClass);
    }
예제 #2
0
    IXamlAstNode IXamlAstVisitor.Visit(IXamlAstNode node)
    {
        if (node is not XamlAstObjectNode objectNode)
        {
            return(node);
        }

        var clrType           = objectNode.Type.GetClrType();
        var isAvaloniaControl = clrType
                                .Interfaces
                                .Any(abstraction => abstraction.IsInterface &&
                                     abstraction.FullName == "Avalonia.Controls.IControl");

        if (!isAvaloniaControl)
        {
            return(node);
        }

        foreach (var child in objectNode.Children)
        {
            if (child is XamlAstXmlDirective directive &&
                directive.Name == "Class" &&
                directive.Namespace == XamlNamespaces.Xaml2006 &&
                directive.Values[0] is XamlAstTextNode text)
            {
                if (_checkTypeValidity)
                {
                    var existingType = _typeSystem.FindType(text.Text);
                    if (existingType == null)
                    {
                        _onTypeInvalid?.Invoke(text.Text);
                        return(node);
                    }
                }

                var split     = text.Text.Split('.');
                var nameSpace = string.Join(".", split.Take(split.Length - 1));
                var className = split.Last();

                _resolvedClass = new ResolvedView(className, clrType, nameSpace, _xaml);
                return(node);
            }
        }

        return(node);
    }