Пример #1
0
        public static void Load(object view, string xaml)
        {
            using (var textReader = new StringReader(xaml))
                using (var reader = XmlReader.Create(textReader))
                {
                    while (reader.Read())
                    {
                        //Skip until element
                        if (reader.NodeType == XmlNodeType.Whitespace)
                        {
                            continue;
                        }
                        if (reader.NodeType == XmlNodeType.XmlDeclaration)
                        {
                            continue;
                        }
                        if (reader.NodeType != XmlNodeType.Element)
                        {
                            Debug.WriteLine("Unhandled node {0} {1} {2}", reader.NodeType, reader.Name, reader.Value);
                            continue;
                        }

                        for (int i = 0; i < 100; i++)
                        {
                            XmlType temp = new XmlType(reader.NamespaceURI, reader.Name, null);
                        }

                        XmlType xmlType = new XmlType(reader.NamespaceURI, reader.Name, null);

                        for (int i = 0; i < 100; i++)
                        {
                            new RuntimeRootNode(xmlType, view, (IXmlNamespaceResolver)reader);
                        }

                        var rootnode = new RuntimeRootNode(xmlType, view, (IXmlNamespaceResolver)reader);
                        XamlParser.ParseXaml(rootnode, reader);
                        Visit(rootnode, new HydrationContext {
                            RootElement = view,
#pragma warning disable 0618
                            ExceptionHandler = ResourceLoader.ExceptionHandler ?? (Internals.XamlLoader.DoNotThrowOnExceptions ? e => { }: (Action <Exception>)null)
#pragma warning restore 0618
                        });
                        break;
                    }
                }
        }
Пример #2
0
 public EXamlCreateObject ProvideValue(EXamlContext context, ModuleDefinition module)
 {
     if (TypedBinding == null)
     {
         var typeRef = XmlType.GetTypeReference(XmlTypeExtensions.ModeOfGetType.OnlyGetType, module, null);
         return(new EXamlCreateObject(context, null, typeRef, new object[] { Path, ModeInEXaml, Converter, ConverterParameter, StringFormat, Source }));
     }
     else
     {
         throw new Exception("TypedBinding should not be not null");
         //TypedBinding.Mode = Mode;
         //TypedBinding.Converter = Converter;
         //TypedBinding.ConverterParameter = ConverterParameter;
         //TypedBinding.StringFormat = StringFormat;
         //TypedBinding.Source = Source;
         //TypedBinding.UpdateSourceEventName = UpdateSourceEventName;
         //TypedBinding.FallbackValue = FallbackValue;
         //TypedBinding.TargetNullValue = TargetNullValue;
         //return TypedBinding;
     }
 }
Пример #3
0
        public static Type GetElementType(XmlType xmlType, IXmlLineInfo xmlInfo, Assembly currentAssembly,
                                          out XamlParseException exception)
        {
            if (s_xmlnsDefinitions == null)
            {
                GatherXmlnsDefinitionAttributes(currentAssembly);
            }

            var namespaceURI  = xmlType.NamespaceUri;
            var elementName   = xmlType.Name;
            var typeArguments = xmlType.TypeArguments;

            exception = null;

            if (elementName.Contains("-"))
            {
                elementName = elementName.Replace('-', '+');
            }

            var lookupAssemblies = new List <XmlnsDefinitionAttribute>();
            var lookupNames      = new List <string>();

            foreach (var xmlnsDef in s_xmlnsDefinitions)
            {
                if (xmlnsDef.XmlNamespace != namespaceURI)
                {
                    continue;
                }
                lookupAssemblies.Add(xmlnsDef);
            }

            if (lookupAssemblies.Count == 0)
            {
                string ns, asmstring, _;
                XmlnsHelper.ParseXmlns(namespaceURI, out _, out ns, out asmstring);
                lookupAssemblies.Add(new XmlnsDefinitionAttribute(namespaceURI, ns)
                {
                    AssemblyName = asmstring ?? currentAssembly.FullName
                });
            }

            lookupNames.Add(elementName);
            lookupNames.Add(elementName + "Extension");

            for (var i = 0; i < lookupNames.Count; i++)
            {
                var name = lookupNames[i];
                if (name.Contains(":"))
                {
                    name = name.Substring(name.LastIndexOf(':') + 1);
                }
                if (typeArguments != null)
                {
                    name += "`" + typeArguments.Count; //this will return an open generic Type
                }
                lookupNames[i] = name;
            }

            Type type = null;

            foreach (var asm in lookupAssemblies)
            {
                foreach (var name in lookupNames)
                {
                    if ((type = Type.GetType($"{asm.ClrNamespace}.{name}, {asm.AssemblyName}")) != null)
                    {
                        break;
                    }

                    if ('?' == name.Last())
                    {
                        string nameOfNotNull = name.Substring(0, name.Length - 1);
                        Type   typeofNotNull = Type.GetType($"{asm.ClrNamespace}.{nameOfNotNull}, {asm.AssemblyName}");

                        if (null != typeofNotNull)
                        {
                            type = typeof(Nullable <>).MakeGenericType(new Type[] { typeofNotNull });
                            break;
                        }
                    }
                }

                if (type != null)
                {
                    break;
                }
            }

            if (type != null && typeArguments != null)
            {
                XamlParseException innerexception = null;
                var args = typeArguments.Select(delegate(XmlType xmltype)
                {
                    XamlParseException xpe;
                    var t = GetElementType(xmltype, xmlInfo, currentAssembly, out xpe);
                    if (xpe != null)
                    {
                        innerexception = xpe;
                        return(null);
                    }
                    return(t);
                }).ToArray();
                if (innerexception != null)
                {
                    exception = innerexception;
                    return(null);
                }
                type = type.MakeGenericType(args);
            }

            if (type == null)
            {
                var message = $"Type {elementName} not found in xmlns {namespaceURI}\n";
                message  += "\n  - Make sure the all used assemblies (e.g. Tizen.NUI.Components) are included in the application project.";
                message  += "\n  - Make sure the type and namespace are correct.\n";
                exception = new XamlParseException($"message", xmlInfo);
            }

            return(type);
        }
Пример #4
0
        public static Type GetElementType(XmlType xmlType, IXmlLineInfo xmlInfo, Assembly currentAssembly,
                                          out XamlParseException exception)
        {
            if (s_xmlnsDefinitions == null)
            {
                GatherXmlnsDefinitionAttributes();
            }

            var namespaceURI  = xmlType.NamespaceUri;
            var elementName   = xmlType.Name;
            var typeArguments = xmlType.TypeArguments;

            exception = null;

            if (elementName.Contains("-"))
            {
                elementName = elementName.Replace('-', '+');
            }

            var lookupAssemblies = new List <XmlnsDefinitionAttribute>();
            var lookupNames      = new List <string>();

            foreach (var xmlnsDef in s_xmlnsDefinitions)
            {
                if (xmlnsDef.XmlNamespace != namespaceURI)
                {
                    continue;
                }
                lookupAssemblies.Add(xmlnsDef);
            }

            if (lookupAssemblies.Count == 0)
            {
                string ns, asmstring, _;
                XmlnsHelper.ParseXmlns(namespaceURI, out _, out ns, out asmstring, out _);
                lookupAssemblies.Add(new XmlnsDefinitionAttribute(namespaceURI, ns, 0)
                {
                    AssemblyName = asmstring ?? currentAssembly.FullName
                });
            }

            lookupNames.Add(elementName);
            lookupNames.Add(elementName + "Extension");

            for (var i = 0; i < lookupNames.Count; i++)
            {
                var name = lookupNames[i];
                if (name.Contains(":"))
                {
                    name = name.Substring(name.LastIndexOf(':') + 1);
                }
                if (typeArguments != null)
                {
                    name += "`" + typeArguments.Count; //this will return an open generic Type
                }
                lookupNames[i] = name;
            }

            Type type = null;

            foreach (var asm in lookupAssemblies)
            {
                foreach (var name in lookupNames)
                {
                    if ((type = Type.GetType($"{asm.ClrNamespace}.{name}, {asm.AssemblyName}")) != null)
                    {
                        break;
                    }
                }
                if (type != null)
                {
                    break;
                }
            }

            if (type != null && typeArguments != null)
            {
                XamlParseException innerexception = null;
                var args = typeArguments.Select(delegate(XmlType xmltype)
                {
                    XamlParseException xpe;
                    var t = GetElementType(xmltype, xmlInfo, currentAssembly, out xpe);
                    if (xpe != null)
                    {
                        innerexception = xpe;
                        return(null);
                    }
                    return(t);
                }).ToArray();
                if (innerexception != null)
                {
                    exception = innerexception;
                    return(null);
                }
                type = type.MakeGenericType(args);
            }

            if (type == null)
            {
                exception = new XamlParseException($"Type {elementName} not found in xmlns {namespaceURI}", xmlInfo);
            }

            return(type);
        }
Пример #5
0
            public INode Parse(string match, ref string remaining, IServiceProvider serviceProvider)
            {
                var nsResolver = serviceProvider.GetService(typeof(IXmlNamespaceResolver)) as IXmlNamespaceResolver;

                if (nsResolver == null)
                {
                    throw new ArgumentException();
                }
                IXmlLineInfo xmlLineInfo         = null;
                var          xmlLineInfoProvider = serviceProvider.GetService(typeof(IXmlLineInfoProvider)) as IXmlLineInfoProvider;

                if (xmlLineInfoProvider != null)
                {
                    xmlLineInfo = xmlLineInfoProvider.XmlLineInfo;
                }

                var split = match.Split(':');

                if (split.Length > 2)
                {
                    throw new ArgumentException();
                }

                string prefix; //, name;

                if (split.Length == 2)
                {
                    prefix = split[0];
                    //					name = split [1];
                }
                else
                {
                    prefix = "";
                    //					name = split [0];
                }

                Type type;
                var  typeResolver = serviceProvider.GetService(typeof(IXamlTypeResolver)) as IXamlTypeResolver;

                if (typeResolver == null)
                {
                    type = null;
                }
                // Add Binding and StaticResource support, The ordinal code can't find BindingExtension for Binding
                //else if (match == "Binding")
                //{
                //    type = typeof(BindingExtension);
                //}
                //else if (match == "StaticResource")
                //{
                //    type = typeof(StaticResourceExtension);
                //}
                else
                {
                    //The order of lookup is to look for the Extension-suffixed class name first and then look for the class name without the Extension suffix.
                    if (!typeResolver.TryResolve(match + "Extension", out type) && !typeResolver.TryResolve(match, out type))
                    {
                        var lineInfoProvider = serviceProvider.GetService(typeof(IXmlLineInfoProvider)) as IXmlLineInfoProvider;
                        var lineInfo         = (lineInfoProvider != null) ? lineInfoProvider.XmlLineInfo : new XmlLineInfo();
                        throw new XamlParseException(String.Format("MarkupExtension not found for {0}", match), lineInfo);
                    }
                }

                var namespaceuri = nsResolver.LookupNamespace(prefix) ?? "";
                var xmltype      = new XmlType(namespaceuri, type?.Name, null);

                if (type == null)
                {
                    throw new NotSupportedException();
                }

                node = xmlLineInfo == null
                    ? new ElementNode(xmltype, null, nsResolver)
                    : new ElementNode(xmltype, null, nsResolver, xmlLineInfo.LineNumber, xmlLineInfo.LinePosition);

                if (remaining.StartsWith("}", StringComparison.Ordinal))
                {
                    remaining = remaining.Substring(1);
                    return(node);
                }

                char   next;
                string piece;

                while ((piece = GetNextPiece(ref remaining, out next)) != null)
                {
                    HandleProperty(piece, serviceProvider, ref remaining, next != '=');
                }

                return(node);
            }
Пример #6
0
 protected RootNode(XmlType xmlType, IXmlNamespaceResolver nsResolver) : base(xmlType, xmlType.NamespaceUri, nsResolver)
 {
 }
Пример #7
0
 public RuntimeRootNode(XmlType xmlType, object root, IXmlNamespaceResolver resolver) : base(xmlType, resolver)
 {
     Root = root;
 }
Пример #8
0
        public static Type GetElementType(XmlType xmlType, IXmlLineInfo xmlInfo, Assembly currentAssembly,
                                          out XamlParseException exception)
        {
            if (s_xmlnsDefinitions == null)
            {
                GatherXmlnsDefinitionAttributes();
            }

            var namespaceURI  = xmlType.NamespaceUri;
            var elementName   = xmlType.Name;
            var typeArguments = xmlType.TypeArguments;

            exception = null;

            var lookupAssemblies = new List <XmlnsDefinitionAttribute>();
            var lookupNames      = new List <string>();

            foreach (var xmlnsDef in s_xmlnsDefinitions)
            {
                if (xmlnsDef.XmlNamespace != namespaceURI)
                {
                    continue;
                }
                lookupAssemblies.Add(xmlnsDef);
            }

            if (lookupAssemblies.Count == 0)
            {
                string ns, asmstring, _;
                XmlnsHelper.ParseXmlns(namespaceURI, out _, out ns, out asmstring, out _);
                lookupAssemblies.Add(new XmlnsDefinitionAttribute(namespaceURI, ns)
                {
                    AssemblyName = asmstring ?? currentAssembly.FullName
                });
            }

            lookupNames.Add(elementName);
            lookupNames.Add(elementName + "Extension");

            for (var i = 0; i < lookupNames.Count; i++)
            {
                var name = lookupNames[i];
                if (name.Contains(":"))
                {
                    name = name.Substring(name.LastIndexOf(':') + 1);
                }
                if (typeArguments != null)
                {
                    name += "`" + typeArguments.Count;                     //this will return an open generic Type
                }
                lookupNames[i] = name;
            }

            Type type = null;

            foreach (var asm in lookupAssemblies)
            {
                foreach (var name in lookupNames)
                {
                    if ((type = Type.GetType($"{asm.ClrNamespace}.{name}, {asm.AssemblyName}")) != null)
                    {
                        break;
                    }
                }
                if (type != null)
                {
                    break;
                }
            }

            if (type == null)
            {
                List <Tuple <string, Assembly> > lookupAssemblies2 = new List <Tuple <string, Assembly> >();
                if (namespaceURI == NUI2018Uri)
                {
                    // Got the type of Tizen.NUI wiedget here, then CreateValueVisitor will create the instance of Tizen.NUI widget
                    lookupAssemblies2.Add(new Tuple <string, Assembly>("Tizen.NUI", typeof(Tizen.NUI.BaseComponents.View).GetTypeInfo().Assembly));
                    lookupAssemblies2.Add(new Tuple <string, Assembly>("Tizen.NUI.BaseComponents", typeof(Tizen.NUI.BaseComponents.View).GetTypeInfo().Assembly));
                    lookupAssemblies2.Add(new Tuple <string, Assembly>("Tizen.NUI.UIComponents", typeof(Tizen.NUI.BaseComponents.View).GetTypeInfo().Assembly));
                    lookupAssemblies2.Add(new Tuple <string, Assembly>("Tizen.NUI.Xaml", typeof(XamlLoader).GetTypeInfo().Assembly));
                    lookupAssemblies2.Add(new Tuple <string, Assembly>("Tizen.NUI.Binding", typeof(Tizen.NUI.BaseComponents.View).GetTypeInfo().Assembly));
                }
                else if (namespaceURI == X2009Uri || namespaceURI == X2006Uri)
                {
                    lookupAssemblies2.Add(new Tuple <string, Assembly>("Tizen.NUI.Xaml", typeof(XamlLoader).GetTypeInfo().Assembly));
                    lookupAssemblies2.Add(new Tuple <string, Assembly>("System", typeof(object).GetTypeInfo().Assembly));
                    lookupAssemblies2.Add(new Tuple <string, Assembly>("System", typeof(Uri).GetTypeInfo().Assembly)); //System.dll
                }
                else
                {
                    string   ns;
                    string   typename;
                    string   asmstring;
                    Assembly asm;
                    XmlnsHelper.ParseXmlns(namespaceURI, out typename, out ns, out asmstring, out _);
                    asm = asmstring == null ? currentAssembly : Assembly.Load(new AssemblyName(asmstring));
                    lookupAssemblies2.Add(new Tuple <string, Assembly>(ns, asm));
                }

                foreach (var asm in lookupAssemblies2)
                {
                    if (type != null)
                    {
                        break;
                    }
                    foreach (var name in lookupNames)
                    {
                        if (type != null)
                        {
                            break;
                        }
                        type = asm.Item2.GetType(asm.Item1 + "." + name);
                    }
                }
            }

            if (type != null && typeArguments != null)
            {
                XamlParseException innerexception = null;
                var args = typeArguments.Select(delegate(XmlType xmltype)
                {
                    XamlParseException xpe;
                    var t = GetElementType(xmltype, xmlInfo, currentAssembly, out xpe);
                    if (xpe != null)
                    {
                        innerexception = xpe;
                        return(null);
                    }
                    return(t);
                }).ToArray();
                if (innerexception != null)
                {
                    exception = innerexception;
                    return(null);
                }
                type = type.MakeGenericType(args);
            }

            if (type == null)
            {
                exception = new XamlParseException($"Type {elementName} not found in xmlns {namespaceURI}", xmlInfo);
            }

            return(type);
        }