diff --git a/src/Controls/samples/Controls.Sample.UITests/Controls.Sample.UITests.csproj b/src/Controls/samples/Controls.Sample.UITests/Controls.Sample.UITests.csproj index b57f275204e7..0854fd5784ab 100644 --- a/src/Controls/samples/Controls.Sample.UITests/Controls.Sample.UITests.csproj +++ b/src/Controls/samples/Controls.Sample.UITests/Controls.Sample.UITests.csproj @@ -23,6 +23,7 @@ true <_IsPublishing>true false + IL3050;XC0022 $(DefineConstants);NATIVE_AOT diff --git a/src/Controls/src/Build.Tasks/SetPropertiesVisitor.cs b/src/Controls/src/Build.Tasks/SetPropertiesVisitor.cs index d041d10f0307..76d099ab1f74 100644 --- a/src/Controls/src/Build.Tasks/SetPropertiesVisitor.cs +++ b/src/Controls/src/Build.Tasks/SetPropertiesVisitor.cs @@ -517,7 +517,7 @@ static bool IsBindingContextBinding(ElementNode node) { // looking for BindingContext="{Binding ...}" return GetParent(node) is IElementNode parentNode - && ApplyPropertiesVisitor.TryGetPropertyName(node, parentNode, out var propertyName) + && node.TryGetPropertyName(parentNode, out var propertyName) && propertyName.NamespaceURI == "" && propertyName.LocalName == nameof(BindableObject.BindingContext); } diff --git a/src/Controls/src/SourceGen/CodeBehindGenerator.cs b/src/Controls/src/SourceGen/CodeBehindGenerator.cs index 844d80fd900f..d511dc445147 100644 --- a/src/Controls/src/SourceGen/CodeBehindGenerator.cs +++ b/src/Controls/src/SourceGen/CodeBehindGenerator.cs @@ -358,9 +358,9 @@ static void GenerateXamlCodeBehind(XamlProjectItem? xamlItem, Compilation compil sb.AppendLine("\t\tprivate void InitializeComponent()"); sb.AppendLine("\t\t{"); - sb.AppendLine("#pragma warning disable IL2026 // The body of InitializeComponent will be replaced by XamlC so LoadFromXaml will never be called in production builds"); + sb.AppendLine("#pragma warning disable IL2026, IL3050 // The body of InitializeComponent will be replaced by XamlC so LoadFromXaml will never be called in production builds"); sb.AppendLine($"\t\t\tglobal::Microsoft.Maui.Controls.Xaml.Extensions.LoadFromXaml(this, typeof({rootType}));"); - sb.AppendLine("#pragma warning restore IL2026"); + sb.AppendLine("#pragma warning restore IL2026, IL3050"); if (namedFields != null) { diff --git a/src/Controls/src/Xaml/ApplyPropertiesVisitor.cs b/src/Controls/src/Xaml/ApplyPropertiesVisitor.cs index 949df4596f62..7243af5f05b6 100644 --- a/src/Controls/src/Xaml/ApplyPropertiesVisitor.cs +++ b/src/Controls/src/Xaml/ApplyPropertiesVisitor.cs @@ -2,6 +2,7 @@ using System.Collections; using System.Collections.Generic; using System.ComponentModel; +using System.Diagnostics.CodeAnalysis; using System.Linq; using System.Reflection; using System.Xml; @@ -14,6 +15,10 @@ namespace Microsoft.Maui.Controls.Xaml { + [RequiresUnreferencedCode(TrimmerConstants.XamlRuntimeParsingNotSupportedWarning)] +#if !NETSTANDARD + [RequiresDynamicCode(TrimmerConstants.XamlRuntimeParsingNotSupportedWarning)] +#endif class ApplyPropertiesVisitor : IXamlNodeVisitor { public static readonly IList Skips = new List { @@ -49,7 +54,7 @@ public void Visit(ValueNode node, INode parentNode) return; - if (TryGetPropertyName(node, parentNode, out XmlName propertyName)) + if (node.TryGetPropertyName(parentNode, out XmlName propertyName)) { if (TrySetRuntimeName(propertyName, source, value, node)) return; @@ -83,7 +88,7 @@ public void Visit(MarkupNode node, INode parentNode) public void Visit(ElementNode node, INode parentNode) { - if (TryGetPropertyName(node, parentNode, out XmlName propertyName) && propertyName == XmlName._CreateContent) + if (node.TryGetPropertyName(parentNode, out XmlName propertyName) && propertyName == XmlName._CreateContent) { var s0 = Values[parentNode]; if (s0 is ElementTemplate) @@ -107,7 +112,7 @@ public void Visit(ElementNode node, INode parentNode) if (!Values.TryGetValue(node, out var value) && Context.ExceptionHandler != null) return; - if (propertyName != XmlName.Empty || TryGetPropertyName(node, parentNode, out propertyName)) + if (propertyName != XmlName.Empty || node.TryGetPropertyName(parentNode, out propertyName)) { if (Skips.Contains(propertyName)) return; @@ -229,22 +234,6 @@ public void Visit(ListNode node, INode parentNode) { } - public static bool TryGetPropertyName(INode node, INode parentNode, out XmlName name) - { - name = default(XmlName); - var parentElement = parentNode as IElementNode; - if (parentElement == null) - return false; - foreach (var kvp in parentElement.Properties) - { - if (kvp.Value != node) - continue; - name = kvp.Key; - return true; - } - return false; - } - internal static bool IsCollectionItem(INode node, INode parentNode) { var parentList = parentNode as IListNode; diff --git a/src/Controls/src/Xaml/Controls.Xaml.csproj b/src/Controls/src/Xaml/Controls.Xaml.csproj index 58f68a6fb626..e0d0a5df2288 100644 --- a/src/Controls/src/Xaml/Controls.Xaml.csproj +++ b/src/Controls/src/Xaml/Controls.Xaml.csproj @@ -22,6 +22,12 @@ .NET Multi-platform App UI (.NET MAUI) is a cross-platform framework for creating native mobile and desktop apps with C# and XAML. This package only contains the XAML tooling. Please install the Microsoft.Maui.Controls package to start using .NET MAUI. + + true + true + true + + diff --git a/src/Controls/src/Xaml/CreateValuesVisitor.cs b/src/Controls/src/Xaml/CreateValuesVisitor.cs index ad97b796a311..a7460a9fc0bb 100644 --- a/src/Controls/src/Xaml/CreateValuesVisitor.cs +++ b/src/Controls/src/Xaml/CreateValuesVisitor.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; using System.Globalization; using System.Linq; using System.Reflection; @@ -9,6 +10,10 @@ namespace Microsoft.Maui.Controls.Xaml { + [RequiresUnreferencedCode(TrimmerConstants.XamlRuntimeParsingNotSupportedWarning)] +#if !NETSTANDARD + [RequiresDynamicCode(TrimmerConstants.XamlRuntimeParsingNotSupportedWarning)] +#endif class CreateValuesVisitor : IXamlNodeVisitor { public CreateValuesVisitor(HydrationContext context) @@ -185,7 +190,7 @@ public void Visit(RootNode node, INode parentNode) public void Visit(ListNode node, INode parentNode) { //this is a gross hack to keep ListNode alive. ListNode must go in favor of Properties - if (ApplyPropertiesVisitor.TryGetPropertyName(node, parentNode, out XmlName name)) + if (node.TryGetPropertyName(parentNode, out XmlName name)) node.XmlName = name; } diff --git a/src/Controls/src/Xaml/ExpandMarkupsVisitor.cs b/src/Controls/src/Xaml/ExpandMarkupsVisitor.cs index 6e462f088891..b94858d40e96 100644 --- a/src/Controls/src/Xaml/ExpandMarkupsVisitor.cs +++ b/src/Controls/src/Xaml/ExpandMarkupsVisitor.cs @@ -1,10 +1,15 @@ using System; using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; using System.Xml; using Microsoft.Maui.Controls.Xaml.Internals; namespace Microsoft.Maui.Controls.Xaml { + [RequiresUnreferencedCode(TrimmerConstants.XamlRuntimeParsingNotSupportedWarning)] +#if !NETSTANDARD + [RequiresDynamicCode(TrimmerConstants.XamlRuntimeParsingNotSupportedWarning)] +#endif class ExpandMarkupsVisitor : IXamlNodeVisitor { public ExpandMarkupsVisitor(HydrationContext context) => Context = context; @@ -40,7 +45,7 @@ public void Visit(MarkupNode markupnode, INode parentNode) { var parentElement = parentNode as IElementNode; XmlName propertyName; - if (!ApplyPropertiesVisitor.TryGetPropertyName(markupnode, parentNode, out propertyName)) + if (!markupnode.TryGetPropertyName(parentNode, out propertyName)) return; if (Skips.Contains(propertyName)) return; @@ -106,6 +111,7 @@ INode ParseExpression(ref string expression, IXmlNamespaceResolver nsResolver, I return new MarkupExpansionParser { ExceptionHandler = Context.ExceptionHandler }.Parse(match, ref expression, serviceProvider); } + [RequiresUnreferencedCode(TrimmerConstants.XamlRuntimeParsingNotSupportedWarning)] public class MarkupExpansionParser : MarkupExpressionParser, IExpressionParser { IElementNode _node; diff --git a/src/Controls/src/Xaml/FillResourceDictionariesVisitor.cs b/src/Controls/src/Xaml/FillResourceDictionariesVisitor.cs index ff2bf36d305c..68229b2cb48c 100644 --- a/src/Controls/src/Xaml/FillResourceDictionariesVisitor.cs +++ b/src/Controls/src/Xaml/FillResourceDictionariesVisitor.cs @@ -1,11 +1,16 @@ using System; using System.Collections; using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; using Microsoft.Maui.Controls.Internals; using Microsoft.Maui.Controls.Xaml.Internals; namespace Microsoft.Maui.Controls.Xaml { + [RequiresUnreferencedCode(TrimmerConstants.XamlRuntimeParsingNotSupportedWarning)] +#if !NETSTANDARD + [RequiresDynamicCode(TrimmerConstants.XamlRuntimeParsingNotSupportedWarning)] +#endif class FillResourceDictionariesVisitor : IXamlNodeVisitor { public FillResourceDictionariesVisitor(HydrationContext context) => Context = context; @@ -38,7 +43,7 @@ public void Visit(ElementNode node, INode parentNode) return; //Set RD to VE - if (typeof(ResourceDictionary).IsAssignableFrom(Context.Types[node]) && ApplyPropertiesVisitor.TryGetPropertyName(node, parentNode, out XmlName propertyName)) + if (typeof(ResourceDictionary).IsAssignableFrom(Context.Types[node]) && node.TryGetPropertyName(parentNode, out XmlName propertyName)) { if ((propertyName.LocalName == "Resources" || propertyName.LocalName.EndsWith(".Resources", StringComparison.Ordinal)) && value is ResourceDictionary) diff --git a/src/Controls/src/Xaml/MarkupExpressionParser.cs b/src/Controls/src/Xaml/MarkupExpressionParser.cs index cc5100f42146..6042dea75697 100644 --- a/src/Controls/src/Xaml/MarkupExpressionParser.cs +++ b/src/Controls/src/Xaml/MarkupExpressionParser.cs @@ -32,6 +32,7 @@ // using System; +using System.Diagnostics.CodeAnalysis; using System.Text; namespace Microsoft.Maui.Controls.Xaml @@ -46,6 +47,7 @@ protected struct Property public object value; } + [RequiresUnreferencedCode(TrimmerConstants.XamlRuntimeParsingNotSupportedWarning)] public object ParseExpression(ref string expression, IServiceProvider serviceProvider) { if (serviceProvider == null) @@ -120,6 +122,7 @@ internal static bool MatchMarkup(out string match, string expression, out int en return true; } + [RequiresUnreferencedCode(TrimmerConstants.XamlRuntimeParsingNotSupportedWarning)] protected Property ParseProperty(IServiceProvider serviceProvider, ref string remaining) { object value = null; @@ -148,6 +151,7 @@ protected Property ParseProperty(IServiceProvider serviceProvider, ref string re return new Property { last = next == '}', name = name, strValue = str_value, value = value }; } + [RequiresUnreferencedCode(TrimmerConstants.XamlRuntimeParsingNotSupportedWarning)] Property ParsePropertyExpression(string prop, IServiceProvider serviceProvider, ref string remaining) { bool last; diff --git a/src/Controls/src/Xaml/MarkupExtensionParser.cs b/src/Controls/src/Xaml/MarkupExtensionParser.cs index 3d51baf75c63..e3b53e4f2e24 100644 --- a/src/Controls/src/Xaml/MarkupExtensionParser.cs +++ b/src/Controls/src/Xaml/MarkupExtensionParser.cs @@ -1,9 +1,14 @@ using System; using System.ComponentModel; +using System.Diagnostics.CodeAnalysis; using System.Reflection; namespace Microsoft.Maui.Controls.Xaml { + [RequiresUnreferencedCode(TrimmerConstants.XamlRuntimeParsingNotSupportedWarning)] +#if !NETSTANDARD + [RequiresDynamicCode(TrimmerConstants.XamlRuntimeParsingNotSupportedWarning)] +#endif internal sealed class MarkupExtensionParser : MarkupExpressionParser, IExpressionParser { IMarkupExtension markupExtension; diff --git a/src/Controls/src/Xaml/MarkupExtensions/ArrayExtension.cs b/src/Controls/src/Xaml/MarkupExtensions/ArrayExtension.cs index fe3a071736a0..00cfb14d3186 100644 --- a/src/Controls/src/Xaml/MarkupExtensions/ArrayExtension.cs +++ b/src/Controls/src/Xaml/MarkupExtensions/ArrayExtension.cs @@ -1,11 +1,15 @@ using System; using System.Collections; using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; namespace Microsoft.Maui.Controls.Xaml { [ContentProperty(nameof(Items))] [AcceptEmptyServiceProvider] +#if !NETSTANDARD + [RequiresDynamicCode("ArrayExtension is not AOT safe.")] +#endif public class ArrayExtension : IMarkupExtension { public ArrayExtension() diff --git a/src/Controls/src/Xaml/MarkupExtensions/BindingExtension.cs b/src/Controls/src/Xaml/MarkupExtensions/BindingExtension.cs index 2ef129a53e9e..32ba766e14b8 100644 --- a/src/Controls/src/Xaml/MarkupExtensions/BindingExtension.cs +++ b/src/Controls/src/Xaml/MarkupExtensions/BindingExtension.cs @@ -22,12 +22,18 @@ public sealed class BindingExtension : IMarkupExtension BindingBase IMarkupExtension.ProvideValue(IServiceProvider serviceProvider) { if (TypedBinding == null) + { +#pragma warning disable IL2026 // Using member 'Microsoft.Maui.Controls.Binding.Binding(String, BindingMode, IValueConverter, Object, String, Object)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Using bindings with string paths is not trim safe. Use expression-based binding instead. + // This code is only reachable in XamlC compiled code when there is a missing x:DataType and the binding could not be compiled. + // In that case, we produce a warning that the binding could not be compiled. return new Binding(Path, Mode, Converter, ConverterParameter, StringFormat, Source) { UpdateSourceEventName = UpdateSourceEventName, FallbackValue = FallbackValue, TargetNullValue = TargetNullValue, }; +#pragma warning restore IL2026 + } TypedBinding.Mode = Mode; TypedBinding.Converter = Converter; diff --git a/src/Controls/src/Xaml/MarkupExtensions/OnIdiomExtension.cs b/src/Controls/src/Xaml/MarkupExtensions/OnIdiomExtension.cs index 3fa9bcac4545..c92eef52a5c7 100644 --- a/src/Controls/src/Xaml/MarkupExtensions/OnIdiomExtension.cs +++ b/src/Controls/src/Xaml/MarkupExtensions/OnIdiomExtension.cs @@ -1,4 +1,5 @@ using System; +using System.Diagnostics.CodeAnalysis; using System.Globalization; using System.Reflection; using System.Xml; @@ -13,6 +14,7 @@ namespace Microsoft.Maui.Controls.Xaml typeof(IValueConverterProvider), typeof(IXmlLineInfoProvider), typeof(IConverterOptions)])] + [RequiresUnreferencedCode("The OnIdiomExtension is not trim safe. Use OnIdiom instead.")] public class OnIdiomExtension : IMarkupExtension { // See Device.Idiom diff --git a/src/Controls/src/Xaml/MarkupExtensions/OnPlatformExtension.cs b/src/Controls/src/Xaml/MarkupExtensions/OnPlatformExtension.cs index 9a9f36f30810..80cb64c8bbb4 100644 --- a/src/Controls/src/Xaml/MarkupExtensions/OnPlatformExtension.cs +++ b/src/Controls/src/Xaml/MarkupExtensions/OnPlatformExtension.cs @@ -1,4 +1,5 @@ using System; +using System.Diagnostics.CodeAnalysis; using System.Globalization; using System.Reflection; using Microsoft.Extensions.DependencyInjection; @@ -12,6 +13,7 @@ namespace Microsoft.Maui.Controls.Xaml typeof(IValueConverterProvider), typeof(IXmlLineInfoProvider), typeof(IConverterOptions)])] + [RequiresUnreferencedCode("The OnPlatformExtension is not trim safe. Use OnPlatform instead.")] public class OnPlatformExtension : IMarkupExtension { static object s_notset = new object(); diff --git a/src/Controls/src/Xaml/MarkupExtensions/StaticExtension.cs b/src/Controls/src/Xaml/MarkupExtensions/StaticExtension.cs index ea1c7b8f6aa2..6f878f2e04bf 100644 --- a/src/Controls/src/Xaml/MarkupExtensions/StaticExtension.cs +++ b/src/Controls/src/Xaml/MarkupExtensions/StaticExtension.cs @@ -1,4 +1,5 @@ using System; +using System.Diagnostics.CodeAnalysis; using System.Linq; using System.Reflection; using System.Xml; @@ -7,6 +8,7 @@ namespace Microsoft.Maui.Controls.Xaml { [ContentProperty(nameof(Member))] [ProvideCompiled("Microsoft.Maui.Controls.Build.Tasks.StaticExtension")] + [RequiresUnreferencedCode(TrimmerConstants.XamlRuntimeParsingNotSupportedWarning)] public class StaticExtension : IMarkupExtension { public string Member { get; set; } diff --git a/src/Controls/src/Xaml/MarkupExtensions/TemplateBindingExtension.cs b/src/Controls/src/Xaml/MarkupExtensions/TemplateBindingExtension.cs index 42a3582d4a74..1a6782a216a7 100644 --- a/src/Controls/src/Xaml/MarkupExtensions/TemplateBindingExtension.cs +++ b/src/Controls/src/Xaml/MarkupExtensions/TemplateBindingExtension.cs @@ -1,9 +1,11 @@ using System; +using System.Diagnostics.CodeAnalysis; namespace Microsoft.Maui.Controls.Xaml { [ContentProperty(nameof(Path))] [AcceptEmptyServiceProvider] + [RequiresUnreferencedCode(TrimmerConstants.StringPathBindingWarning, Url = TrimmerConstants.ExpressionBasedBindingsDocsUrl)] public sealed class TemplateBindingExtension : IMarkupExtension { public TemplateBindingExtension() diff --git a/src/Controls/src/Xaml/NodeExtensions.cs b/src/Controls/src/Xaml/NodeExtensions.cs new file mode 100644 index 000000000000..2fac8afcc859 --- /dev/null +++ b/src/Controls/src/Xaml/NodeExtensions.cs @@ -0,0 +1,27 @@ +namespace Microsoft.Maui.Controls.Xaml; + +internal static class NodeExtensions +{ + public static bool TryGetPropertyName(this INode node, INode parentNode, out XmlName name) + { + name = default; + + if (parentNode is not IElementNode parentElement) + { + return false; + } + + foreach (var kvp in parentElement.Properties) + { + if (kvp.Value != node) + { + continue; + } + + name = kvp.Key; + return true; + } + + return false; + } +} diff --git a/src/Controls/src/Xaml/ResourceDictionaryHelpers.cs b/src/Controls/src/Xaml/ResourceDictionaryHelpers.cs index dc3cca43d952..548c3c338ae4 100644 --- a/src/Controls/src/Xaml/ResourceDictionaryHelpers.cs +++ b/src/Controls/src/Xaml/ResourceDictionaryHelpers.cs @@ -1,4 +1,5 @@ using System; +using System.Diagnostics.CodeAnalysis; using System.IO; using System.Reflection; using System.Xml; @@ -7,6 +8,10 @@ namespace Microsoft.Maui.Controls.Xaml { internal static class ResourceDictionaryHelpers { + [RequiresUnreferencedCode(TrimmerConstants.XamlRuntimeParsingNotSupportedWarning)] +#if !NETSTANDARD + [RequiresDynamicCode(TrimmerConstants.XamlRuntimeParsingNotSupportedWarning)] +#endif internal static void LoadFromSource(ResourceDictionary rd, string value, Type rootType, IXmlLineInfo lineInfo) { (value, var assembly) = ResourceDictionary.RDSourceTypeConverter.SplitUriAndAssembly(value, rootType.Assembly); diff --git a/src/Controls/src/Xaml/SimplifyOnPlatformVisitor.cs b/src/Controls/src/Xaml/SimplifyOnPlatformVisitor.cs index 3ba35e8739b0..8a9a46c6a0f4 100644 --- a/src/Controls/src/Xaml/SimplifyOnPlatformVisitor.cs +++ b/src/Controls/src/Xaml/SimplifyOnPlatformVisitor.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Linq; #nullable disable @@ -52,21 +52,21 @@ public void Visit(ElementNode node, INode parentNode) if (node.Properties.TryGetValue(new XmlName("", Target), out INode targetNode) || node.Properties.TryGetValue(new XmlName("", nameof(OnPlatformExtension.Default)), out targetNode)) { - if (!ApplyPropertiesVisitor.TryGetPropertyName(node, parentNode, out XmlName name)) + if (!node.TryGetPropertyName(parentNode, out XmlName name)) return; if (parentNode is IElementNode parentEnode) parentEnode.Properties[name] = targetNode; } else if (node.CollectionItems.Count > 0) // syntax like {OnPlatform foo, iOS=bar} { - if (!ApplyPropertiesVisitor.TryGetPropertyName(node, parentNode, out XmlName name)) + if (!node.TryGetPropertyName(parentNode, out XmlName name)) return; if (parentNode is IElementNode parentEnode) parentEnode.Properties[name] = node.CollectionItems[0]; } else //no prop for target and no Default set { - if (!ApplyPropertiesVisitor.TryGetPropertyName(node, parentNode, out XmlName name)) + if (!node.TryGetPropertyName(parentNode, out XmlName name)) return; //if there's no value for the targetPlatform, ignore the node. //this is slightly different than what OnPlatform does (return default(T)) @@ -81,7 +81,7 @@ public void Visit(ElementNode node, INode parentNode) // var onNode = GetOnNode(node, Target) ?? GetDefault(node); // //Property node - // if (ApplyPropertiesVisitor.TryGetPropertyName(node, parentNode, out XmlName name) + // if (node.TryGetPropertyName(parentNode, out XmlName name) // && parentNode is IElementNode parentEnode) // { // if (onNode != null) diff --git a/src/Controls/src/Xaml/SimplifyTypeExtensionVisitor.cs b/src/Controls/src/Xaml/SimplifyTypeExtensionVisitor.cs index 02a5139de95b..ba4f4ec2d476 100644 --- a/src/Controls/src/Xaml/SimplifyTypeExtensionVisitor.cs +++ b/src/Controls/src/Xaml/SimplifyTypeExtensionVisitor.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Linq; #nullable disable @@ -36,7 +36,7 @@ public void Visit(ElementNode node, INode parentNode) } static bool IsValueOfXDataTypeOrTargetType(ElementNode node, INode parentNode, out XmlName propertyName) - => ApplyPropertiesVisitor.TryGetPropertyName(node, parentNode, out propertyName) + => node.TryGetPropertyName(parentNode, out propertyName) && (IsXDataType(propertyName) || IsTargetTypePropertyOfMauiType(parentNode, propertyName)); static bool IsTargetTypePropertyOfMauiType(INode parentNode, XmlName propertyName) diff --git a/src/Controls/src/Xaml/ViewExtensions.cs b/src/Controls/src/Xaml/ViewExtensions.cs index 747e33a0fc5f..047c0058d595 100644 --- a/src/Controls/src/Xaml/ViewExtensions.cs +++ b/src/Controls/src/Xaml/ViewExtensions.cs @@ -31,23 +31,24 @@ namespace Microsoft.Maui.Controls.Xaml { + [RequiresUnreferencedCode(TrimmerConstants.XamlRuntimeParsingNotSupportedWarning)] +#if !NETSTANDARD + [RequiresDynamicCode(TrimmerConstants.XamlRuntimeParsingNotSupportedWarning)] +#endif public static class Extensions { - [RequiresUnreferencedCode(TrimmerConstants.XamlRuntimeParsingNotSupportedWarning)] public static TXaml LoadFromXaml(this TXaml view, Type callingType) { XamlLoader.Load(view, callingType); return view; } - [RequiresUnreferencedCode(TrimmerConstants.XamlRuntimeParsingNotSupportedWarning)] public static TXaml LoadFromXaml(this TXaml view, string xaml) { XamlLoader.Load(view, xaml); return view; } - [RequiresUnreferencedCode(TrimmerConstants.XamlRuntimeParsingNotSupportedWarning)] internal static TXaml LoadFromXaml(this TXaml view, string xaml, Assembly rootAssembly) { XamlLoader.Load(view, xaml, rootAssembly); diff --git a/src/Controls/src/Xaml/XamlLoader.cs b/src/Controls/src/Xaml/XamlLoader.cs index c44eeb5ec2bd..115044a21bf7 100644 --- a/src/Controls/src/Xaml/XamlLoader.cs +++ b/src/Controls/src/Xaml/XamlLoader.cs @@ -29,6 +29,7 @@ using System.Collections.Generic; using System.ComponentModel; using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; using System.IO; using System.Linq; using System.Reflection; @@ -38,6 +39,10 @@ namespace Microsoft.Maui.Controls.Xaml { + [RequiresUnreferencedCode(TrimmerConstants.XamlRuntimeParsingNotSupportedWarning)] +#if !NETSTANDARD + [RequiresDynamicCode(TrimmerConstants.XamlRuntimeParsingNotSupportedWarning)] +#endif static partial class XamlLoader { public static void Load(object view, Type callingType) diff --git a/src/Controls/src/Xaml/XamlParser.cs b/src/Controls/src/Xaml/XamlParser.cs index e2bdabf2dc5a..0d9672674c3a 100644 --- a/src/Controls/src/Xaml/XamlParser.cs +++ b/src/Controls/src/Xaml/XamlParser.cs @@ -357,6 +357,10 @@ static void GatherXmlnsDefinitionAttributes() } } + [RequiresUnreferencedCode(TrimmerConstants.XamlRuntimeParsingNotSupportedWarning)] +#if !NETSTANDARD + [RequiresDynamicCode(TrimmerConstants.XamlRuntimeParsingNotSupportedWarning)] +#endif public static Type GetElementType(XmlType xmlType, IXmlLineInfo xmlInfo, Assembly currentAssembly, out XamlParseException exception) { @@ -369,6 +373,9 @@ public static Type GetElementType(XmlType xmlType, IXmlLineInfo xmlInfo, Assembl } [RequiresUnreferencedCode(TrimmerConstants.XamlRuntimeParsingNotSupportedWarning)] +#if !NETSTANDARD + [RequiresDynamicCode(TrimmerConstants.XamlRuntimeParsingNotSupportedWarning)] +#endif private static Type GetElementTypeCore(XmlType xmlType, IXmlLineInfo xmlInfo, Assembly currentAssembly, out XamlParseException exception) { diff --git a/src/Controls/src/Xaml/XamlServiceProvider.cs b/src/Controls/src/Xaml/XamlServiceProvider.cs index bf1c18cb8503..8fa2e1eaab28 100644 --- a/src/Controls/src/Xaml/XamlServiceProvider.cs +++ b/src/Controls/src/Xaml/XamlServiceProvider.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.ComponentModel; +using System.Diagnostics.CodeAnalysis; using System.Reflection; using System.Xml; using Microsoft.Maui.Controls.Internals; @@ -13,6 +14,10 @@ public class XamlServiceProvider : IServiceProvider static IValueConverterProvider defaultValueConverterProvider = new ValueConverterProvider(); + [RequiresUnreferencedCode(TrimmerConstants.XamlRuntimeParsingNotSupportedWarning)] +#if !NETSTANDARD + [RequiresDynamicCode(TrimmerConstants.XamlRuntimeParsingNotSupportedWarning)] +#endif internal XamlServiceProvider(INode node, HydrationContext context) { if (context != null && node != null && node.Parent != null && context.Values.TryGetValue(node.Parent, out object targetObject)) @@ -181,6 +186,10 @@ public class XamlTypeResolver : IXamlTypeResolver readonly GetTypeFromXmlName getTypeFromXmlName; readonly IXmlNamespaceResolver namespaceResolver; + [RequiresUnreferencedCode(TrimmerConstants.XamlRuntimeParsingNotSupportedWarning)] +#if !NETSTANDARD + [RequiresDynamicCode(TrimmerConstants.XamlRuntimeParsingNotSupportedWarning)] +#endif public XamlTypeResolver(IXmlNamespaceResolver namespaceResolver, Assembly currentAssembly) : this(namespaceResolver, XamlParser.GetElementType, currentAssembly) { diff --git a/src/Core/src/RuntimeFeature.cs b/src/Core/src/RuntimeFeature.cs index 653fc47a5d39..4d75fd88f427 100644 --- a/src/Core/src/RuntimeFeature.cs +++ b/src/Core/src/RuntimeFeature.cs @@ -24,6 +24,7 @@ internal static class RuntimeFeature #if !NETSTANDARD [FeatureSwitchDefinition("Microsoft.Maui.RuntimeFeature.IsXamlRuntimeParsingSupported")] [FeatureGuard(typeof(RequiresUnreferencedCodeAttribute))] + [FeatureGuard(typeof(RequiresDynamicCodeAttribute))] #endif internal static bool IsXamlRuntimeParsingSupported => AppContext.TryGetSwitch("Microsoft.Maui.RuntimeFeature.IsXamlRuntimeParsingSupported", out bool isSupported)