diff --git a/src/Controls/src/Build.Tasks/SetPropertiesVisitor.cs b/src/Controls/src/Build.Tasks/SetPropertiesVisitor.cs index b823f24dc347..165e18930d62 100644 --- a/src/Controls/src/Build.Tasks/SetPropertiesVisitor.cs +++ b/src/Controls/src/Build.Tasks/SetPropertiesVisitor.cs @@ -307,6 +307,9 @@ public static IEnumerable ProvideValue(VariableDefinitionReference { if (TryCompileBindingPath(node, context, vardefref.VariableDefinition, bindingExtensionType.Value, isStandaloneBinding: bpRef is null, out var instructions)) { + // if the binding is compiled, there's no need to pass the ServiceProvider to the extension + acceptEmptyServiceProvider = true; + foreach (var instruction in instructions) yield return instruction; } diff --git a/src/Controls/src/Build.Tasks/XamlCTask.cs b/src/Controls/src/Build.Tasks/XamlCTask.cs index 829d675b0399..af0600732012 100644 --- a/src/Controls/src/Build.Tasks/XamlCTask.cs +++ b/src/Controls/src/Build.Tasks/XamlCTask.cs @@ -151,6 +151,8 @@ public class XamlCTask : XamlTask /// public bool ValidateOnly { get; set; } + internal bool GenerateFullILInValidateOnlyMode { get; set; } + public override bool Execute(out IList thrownExceptions) { thrownExceptions = null; @@ -415,7 +417,7 @@ bool TryCoreCompile(MethodDefinition initComp, ILRootNode rootnode, string xamlF { XamlFilePath = xamlFilePath, LoggingHelper = loggingHelper, - ValidateOnly = ValidateOnly, + ValidateOnly = ValidateOnly && !GenerateFullILInValidateOnlyMode, CompileBindingsWithSource = CompileBindingsWithSource, }; diff --git a/src/Controls/tests/Xaml.UnitTests/Issues/Maui25406.xaml b/src/Controls/tests/Xaml.UnitTests/Issues/Maui25406.xaml new file mode 100644 index 000000000000..a5e1aa8e76b9 --- /dev/null +++ b/src/Controls/tests/Xaml.UnitTests/Issues/Maui25406.xaml @@ -0,0 +1,10 @@ + + + + diff --git a/src/Controls/tests/Xaml.UnitTests/Issues/Maui25406.xaml.cs b/src/Controls/tests/Xaml.UnitTests/Issues/Maui25406.xaml.cs new file mode 100644 index 000000000000..1c2d9b0919b9 --- /dev/null +++ b/src/Controls/tests/Xaml.UnitTests/Issues/Maui25406.xaml.cs @@ -0,0 +1,51 @@ +using System; +using System.Linq; +using Microsoft.Maui.ApplicationModel; +using Microsoft.Maui.Controls.Core.UnitTests; +using Microsoft.Maui.Dispatching; +using Microsoft.Maui.UnitTests; +using Mono.Cecil.Cil; +using NUnit.Framework; + +namespace Microsoft.Maui.Controls.Xaml.UnitTests; + +public partial class Maui25406 : ContentPage +{ + public Maui25406() + { + InitializeComponent(); + } + + public Maui25406(bool useCompiledXaml) + { + //this stub will be replaced at compile time + } + + [TestFixture] + class Test + { + [SetUp] + public void Setup() + { + Application.SetCurrentApplication(new MockApplication()); + DispatcherProvider.SetCurrent(new DispatcherProviderStub()); + } + + [TearDown] public void TearDown() + { + AppInfo.SetCurrent(null); + } + + [Test] + public void WhenBindingIsCompiledBindingExtensionDoesNotReceiveServiceProviderWithXamlTypeResolver() + { + MockCompiler.Compile(typeof(Maui25406), out var md, generateFullIl: false); + Assert.That(!md.Body.Instructions.Any(static i => i.OpCode == OpCodes.Newobj && i.Operand.ToString().Contains("XamlServiceProvider", StringComparison.Ordinal))); + } + } +} + +public class Maui25406ViewModel +{ + public string Text { get; set; } +} diff --git a/src/Controls/tests/Xaml.UnitTests/MockCompiler.cs b/src/Controls/tests/Xaml.UnitTests/MockCompiler.cs index 4a4a41bc6b06..30323b8ee322 100644 --- a/src/Controls/tests/Xaml.UnitTests/MockCompiler.cs +++ b/src/Controls/tests/Xaml.UnitTests/MockCompiler.cs @@ -23,7 +23,8 @@ public static void Compile( out MethodDefinition methodDefinition, string targetFramework = null, bool treatWarningsAsErrors = false, - bool compileBindingsWithSource = true) + bool compileBindingsWithSource = true, + bool generateFullIl = true) { methodDefinition = null; var assembly = type.Assembly.Location; @@ -39,6 +40,7 @@ public static void Compile( OptimizeIL = true, DebugSymbols = false, ValidateOnly = true, + GenerateFullILInValidateOnlyMode = generateFullIl, Type = type.FullName, TargetFramework = targetFramework, TreatWarningsAsErrors = treatWarningsAsErrors,