예제 #1
0
        static IList <string> PrefixesToIgnore(IList <KeyValuePair <string, string> > xmlns)
        {
            var prefixes = new List <string>();

            foreach (var kvp in xmlns)
            {
                var prefix = kvp.Key;

                string typeName = null, ns = null, asm = null, targetPlatform = null;
                XmlnsHelper.ParseXmlns(kvp.Value, out typeName, out ns, out asm, out targetPlatform);
                if (targetPlatform == null)
                {
                    continue;
                }
                try
                {
                    if (targetPlatform != Device.RuntimePlatform)
                    {
                        // Special case for Windows backward compatibility
                        if (targetPlatform == "Windows" && Device.RuntimePlatform == Device.UWP)
                        {
                            continue;
                        }

                        prefixes.Add(prefix);
                    }
                }
                catch (InvalidOperationException)
                {
                    prefixes.Add(prefix);
                }
            }
            return(prefixes);
        }
예제 #2
0
파일: XamlParser.cs 프로젝트: sung-su/maui
        static IList <string> PrefixesToIgnore(IList <KeyValuePair <string, string> > xmlns)
        {
            var prefixes = new List <string>();

            foreach (var kvp in xmlns)
            {
                var prefix = kvp.Key;

                XmlnsHelper.ParseXmlns(kvp.Value, out _, out _, out _, out var targetPlatform);
                if (targetPlatform == null)
                {
                    continue;
                }

                try
                {
                    if (targetPlatform != DeviceInfo.Platform.ToString())
                    {
                        // Special case for Windows backward compatibility
                        if (targetPlatform == "Windows" && DeviceInfo.Platform == DevicePlatform.WinUI)
                        {
                            continue;
                        }

                        prefixes.Add(prefix);
                    }
                }
                catch (InvalidOperationException)
                {
                    prefixes.Add(prefix);
                }
            }
            return(prefixes);
        }
예제 #3
0
        public static T?GetTypeReference <T>(
            this XmlType xmlType,
            IEnumerable <XmlnsDefinitionAttribute> xmlnsDefinitions,
            string defaultAssemblyName,
            Func <XamlLoader.FallbackTypeInfo, T> refFromTypeInfo,
            out IList <XamlLoader.FallbackTypeInfo>?potentialTypes)
            where T : class
        {
            var lookupAssemblies = new List <XmlnsDefinitionAttribute>();
            var namespaceURI     = xmlType.NamespaceUri;
            var elementName      = xmlType.Name;
            var typeArguments    = xmlType.TypeArguments;

            potentialTypes = null;

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

            if (lookupAssemblies.Count == 0)
            {
                XmlnsHelper.ParseXmlns(namespaceURI, out _, out var ns, out var asmstring, out _);
                asmstring = asmstring ?? defaultAssemblyName;
                if (namespaceURI != null && ns != null)
                {
                    lookupAssemblies.Add(new XmlnsDefinitionAttribute(namespaceURI, ns)
                    {
                        AssemblyName = asmstring
                    });
                }
            }

            var lookupNames = new List <string>();

            if (elementName != "DataTemplate" && !elementName.EndsWith("Extension", StringComparison.Ordinal))
            {
                lookupNames.Add(elementName + "Extension");
            }
            lookupNames.Add(elementName);

            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;
            }

            potentialTypes = new List <XamlLoader.FallbackTypeInfo>();
            foreach (string typeName in lookupNames)
            {
                foreach (XmlnsDefinitionAttribute xmlnsDefinitionAttribute in lookupAssemblies)
                {
                    potentialTypes.Add(new XamlLoader.FallbackTypeInfo
                    {
                        ClrNamespace = xmlnsDefinitionAttribute.ClrNamespace,
                        TypeName     = typeName,
                        AssemblyName = xmlnsDefinitionAttribute.AssemblyName,
                        XmlNamespace = xmlnsDefinitionAttribute.XmlNamespace
                    });
                }
            }

            T?type = null;

            foreach (XamlLoader.FallbackTypeInfo typeInfo in potentialTypes)
            {
                if ((type = refFromTypeInfo(typeInfo)) != null)
                {
                    break;
                }
            }

            return(type);
        }