Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions src/Controls/src/Xaml/XamlLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
23 changes: 11 additions & 12 deletions src/Controls/src/Xaml/XamlParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,8 @@ static IValueNode GetValueNode(object value, XmlReader reader)
}

static IList<XmlnsDefinitionAttribute> s_xmlnsDefinitions;

internal static IList<XmlnsPrefixAttribute> s_xmlnsPrefixes;
internal static Dictionary<Assembly, bool> s_allowImplicitXmlns = new();
static bool ValidateProtectedXmlns(string xmlNamespace, string assemblyName)
{
//maui, and x: xmlns are protected
Expand All @@ -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
Expand All @@ -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)
{
Expand All @@ -383,14 +390,6 @@ static void GatherXmlnsDefinitionAttributes(Assembly currentAssembly)
}
}

public static IList<XmlnsPrefixAttribute> GetXmlnsPrefixAttributes()
{
return [.. AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(assembly => assembly.GetCustomAttributes(typeof(XmlnsPrefixAttribute), false)
.OfType<XmlnsPrefixAttribute>()).Distinct()];
}


[RequiresUnreferencedCode(TrimmerConstants.XamlRuntimeParsingNotSupportedWarning)]
#if !NETSTANDARD
[RequiresDynamicCode(TrimmerConstants.XamlRuntimeParsingNotSupportedWarning)]
Expand All @@ -402,7 +401,7 @@ public static Type GetElementType(XmlType xmlType, IXmlLineInfo xmlInfo, Assembl

retry:
if (s_xmlnsDefinitions == null)
GatherXmlnsDefinitionAttributes(currentAssembly);
GatherXmlnsDefinitionAndXmlnsPrefixAttributes(currentAssembly);

IEnumerable<Type> types = xmlType.GetTypeReferences(
s_xmlnsDefinitions,
Expand Down
Loading