diff --git a/src/Controls/src/Xaml/XamlLoader.cs b/src/Controls/src/Xaml/XamlLoader.cs index 82b07624cff9..c4ceb2dcc344 100644 --- a/src/Controls/src/Xaml/XamlLoader.cs +++ b/src/Controls/src/Xaml/XamlLoader.cs @@ -61,15 +61,21 @@ public static void Load(object view, string xaml, Assembly rootAssembly, bool us { rootAssembly ??= view.GetType().Assembly; - var allowImplicitXmlns = rootAssembly.CustomAttributes.Any(a => - a.AttributeType.FullName == "Microsoft.Maui.Controls.Xaml.Internals.AllowImplicitXmlnsDeclarationAttribute" - && (a.ConstructorArguments.Count == 0 || a.ConstructorArguments[0].Value is bool b && b)); + if (XamlParser.s_xmlnsPrefixes == null) + XamlParser.GatherXmlnsDefinitionAndXmlnsPrefixAttributes(rootAssembly); + if (!XamlParser.s_allowImplicitXmlns.TryGetValue(rootAssembly, out var allowImplicitXmlns)) + { + allowImplicitXmlns = rootAssembly.CustomAttributes.Any(a => + a.AttributeType.FullName == "Microsoft.Maui.Controls.Xaml.Internals.AllowImplicitXmlnsDeclarationAttribute" + && (a.ConstructorArguments.Count == 0 || a.ConstructorArguments[0].Value is bool b && b)); + XamlParser.s_allowImplicitXmlns.Add(rootAssembly, allowImplicitXmlns); + } var nsmgr = new XmlNamespaceManager(new NameTable()); if (allowImplicitXmlns) { nsmgr.AddNamespace("", XamlParser.DefaultImplicitUri); - foreach (var xmlnsPrefix in XamlParser.GetXmlnsPrefixAttributes()) + foreach (var xmlnsPrefix in XamlParser.s_xmlnsPrefixes) nsmgr.AddNamespace(xmlnsPrefix.Prefix, xmlnsPrefix.XmlNamespace); } using (var textReader = new StringReader(xaml)) diff --git a/src/Controls/src/Xaml/XamlParser.cs b/src/Controls/src/Xaml/XamlParser.cs index 0934e9f488d2..71b42d1b4dd6 100644 --- a/src/Controls/src/Xaml/XamlParser.cs +++ b/src/Controls/src/Xaml/XamlParser.cs @@ -332,7 +332,8 @@ static IValueNode GetValueNode(object value, XmlReader reader) } static IList s_xmlnsDefinitions; - + internal static IList s_xmlnsPrefixes; + internal static Dictionary s_allowImplicitXmlns = new(); static bool ValidateProtectedXmlns(string xmlNamespace, string assemblyName) { //maui, and x: xmlns are protected @@ -348,11 +349,12 @@ static bool ValidateProtectedXmlns(string xmlNamespace, string assemblyName) return false; } - static void GatherXmlnsDefinitionAttributes(Assembly currentAssembly) + + internal static void GatherXmlnsDefinitionAndXmlnsPrefixAttributes(Assembly currentAssembly) { Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies(); s_xmlnsDefinitions = []; - + s_xmlnsPrefixes = []; foreach (var assembly in assemblies) { try @@ -373,6 +375,11 @@ static void GatherXmlnsDefinitionAttributes(Assembly currentAssembly) } s_xmlnsDefinitions.Add(attribute); } + + foreach (XmlnsPrefixAttribute attribute in assembly.GetCustomAttributes(typeof(XmlnsPrefixAttribute), false)) + { + s_xmlnsPrefixes.Add(attribute); + } } catch (Exception ex) { @@ -383,14 +390,6 @@ static void GatherXmlnsDefinitionAttributes(Assembly currentAssembly) } } - public static IList GetXmlnsPrefixAttributes() - { - return [.. AppDomain.CurrentDomain.GetAssemblies() - .SelectMany(assembly => assembly.GetCustomAttributes(typeof(XmlnsPrefixAttribute), false) - .OfType()).Distinct()]; - } - - [RequiresUnreferencedCode(TrimmerConstants.XamlRuntimeParsingNotSupportedWarning)] #if !NETSTANDARD [RequiresDynamicCode(TrimmerConstants.XamlRuntimeParsingNotSupportedWarning)] @@ -402,7 +401,7 @@ public static Type GetElementType(XmlType xmlType, IXmlLineInfo xmlInfo, Assembl retry: if (s_xmlnsDefinitions == null) - GatherXmlnsDefinitionAttributes(currentAssembly); + GatherXmlnsDefinitionAndXmlnsPrefixAttributes(currentAssembly); IEnumerable types = xmlType.GetTypeReferences( s_xmlnsDefinitions,