Skip to content

Commit

Permalink
[X] Ignore Parent DataType on collection (#24530)
Browse files Browse the repository at this point in the history
* [X] Ignore Parent DataType on collection

Picker.ItemDisplayBinding for example shouldn't inherit parent DataType

- fixes #23989

* fixtrimming

* suppresswarning

* Update src/TestUtils/src/Microsoft.Maui.IntegrationTests/Utilities/BuildWarningsUtilities.cs
  • Loading branch information
StephaneDelcroix authored Sep 25, 2024
1 parent 7005a68 commit 68524c8
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 6 deletions.
38 changes: 33 additions & 5 deletions src/Controls/src/Xaml/XamlServiceProvider.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Reflection;
using System.Xml;
using Microsoft.Maui.Controls.Internals;
Expand Down Expand Up @@ -29,7 +31,7 @@ internal XamlServiceProvider(INode node, HydrationContext context)
IValueConverterProvider = new ValueConverterProvider();

if (node is IElementNode elementNode)
Add(typeof(IXamlDataTypeProvider), new XamlDataTypeProvider(elementNode));
Add(typeof(IXamlDataTypeProvider), new XamlDataTypeProvider(elementNode, context));
}

public XamlServiceProvider() => IValueConverterProvider = new ValueConverterProvider();
Expand Down Expand Up @@ -268,8 +270,15 @@ public string LookupNamespace(string prefix)

class XamlDataTypeProvider : IXamlDataTypeProvider
{
public XamlDataTypeProvider(IElementNode node)
[RequiresUnreferencedCode("XamlDataTypeProvider is not trim and AOT-compatible.")]
#if !NETSTANDARD
[RequiresDynamicCode("XamlDataTypeProvider is not trim and AOT-compatible.")]
#endif
public XamlDataTypeProvider(IElementNode node, HydrationContext context)
{
Context = context;


static IElementNode GetParent(IElementNode node)
{
return node switch
Expand All @@ -289,6 +298,21 @@ static bool IsBindingContextBinding(IElementNode node)
return false;
}

static bool IsBindingBaseProperty(IElementNode node, HydrationContext context)
{
if ( ApplyPropertiesVisitor.TryGetPropertyName(node, node.Parent, out XmlName name)
&& node.Parent is IElementNode parent
&& XamlParser.GetElementType(parent.XmlType,
new XmlLineInfo(((IXmlLineInfo)node).LineNumber, ((IXmlLineInfo)node).LinePosition),
context.RootElement.GetType().Assembly, out var xpe) is Type parentType
&& parentType.GetRuntimeProperties().FirstOrDefault(p => p.Name == name.LocalName) is PropertyInfo propertyInfo
&& propertyInfo.PropertyType == typeof(BindingBase))
{
return true;
}
return false;
}

INode dataTypeNode = null;
IElementNode n = node as IElementNode;

Expand All @@ -305,17 +329,21 @@ static bool IsBindingContextBinding(IElementNode node)

while (n != null)
{

if (n != skipNode && n.Properties.TryGetValue(XmlName.xDataType, out dataTypeNode))
{
break;
}

if (IsBindingBaseProperty(n, context))
{
break;
}
n = GetParent(n);
}
if (dataTypeNode is ValueNode valueNode)
BindingDataType = valueNode.Value as string;

}
public string BindingDataType { get; }
public HydrationContext Context { get; }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<AssemblyName>Microsoft.Maui.Controls.Xaml.UnitTests</AssemblyName>
<WarningLevel>4</WarningLevel>
<NoWarn>$(NoWarn);0672;0219;0414;CS0436;CS0618</NoWarn>
<WarningsNotAsErrors>$(WarningsNotAsErrors);XC0618;XC0022,XC0023</WarningsNotAsErrors>
<WarningsNotAsErrors>$(WarningsNotAsErrors);XC0618;XC0022;XC0023</WarningsNotAsErrors>
<IsPackable>false</IsPackable>
<DisableMSBuildAssemblyCopyCheck>true</DisableMSBuildAssemblyCopyCheck>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,30 @@ public static void AssertWarnings(this List<WarningsPerFile> actualWarnings, Lis
},
}
},
new WarningsPerFile
{
File = "src/Controls/src/Xaml/XamlServiceProvider.cs",
WarningsPerCode = new List<WarningsPerCode>
{
new WarningsPerCode
{
Code = "IL2026",
Messages = new List<string>
{
"Microsoft.Maui.Controls.Xaml.Internals.XamlServiceProvider.XamlServiceProvider(INode,HydrationContext): Using member 'Microsoft.Maui.Controls.Xaml.Internals.XamlDataTypeProvider.XamlDataTypeProvider(IElementNode,HydrationContext)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. XamlDataTypeProvider is not trim and AOT-compatible.",
}
},
new WarningsPerCode
{
Code = "IL3050",
Messages = new List<string>
{
"Microsoft.Maui.Controls.Xaml.Internals.XamlServiceProvider.XamlServiceProvider(INode,HydrationContext): Using member 'Microsoft.Maui.Controls.Xaml.Internals.XamlDataTypeProvider.XamlDataTypeProvider(IElementNode,HydrationContext)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. XamlDataTypeProvider is not trim and AOT-compatible.",
}
},

}
},
};

#region Utility methods for generating the list of expected warnings
Expand Down

0 comments on commit 68524c8

Please sign in to comment.