diff --git a/src/Avalonia.Build.Tasks/XamlCompilerTaskExecutor.cs b/src/Avalonia.Build.Tasks/XamlCompilerTaskExecutor.cs index 05659389b1..5b0322a342 100644 --- a/src/Avalonia.Build.Tasks/XamlCompilerTaskExecutor.cs +++ b/src/Avalonia.Build.Tasks/XamlCompilerTaskExecutor.cs @@ -73,6 +73,10 @@ namespace Avalonia.Build.Tasks asm.MainModule.ImportReference(editorBrowsableAttribute.GetConstructors() .First(c => c.Parameters.Count == 1)); + var runtimeHelpers = typeSystem.GetType("Avalonia.Markup.Xaml.XamlIl.Runtime.XamlIlRuntimeHelpers"); + var getRootServiceProvider = asm.MainModule.ImportReference( + typeSystem.GetTypeReference(runtimeHelpers).Resolve().Methods + .First(x => x.Name == "GetRootServiceProviderV1")); var loaderDispatcherDef = new TypeDefinition("CompiledAvaloniaXaml", "!XamlLoader", TypeAttributes.Class, asm.MainModule.TypeSystem.Object); @@ -158,7 +162,7 @@ namespace Avalonia.Build.Tasks MethodAttributes.Static | MethodAttributes.Private, asm.MainModule.TypeSystem.Void); trampoline.Parameters.Add(new ParameterDefinition(classTypeDefinition)); classTypeDefinition.Methods.Add(trampoline); - trampoline.Body.Instructions.Add(Instruction.Create(OpCodes.Ldnull)); + trampoline.Body.Instructions.Add(Instruction.Create(OpCodes.Call, getRootServiceProvider)); trampoline.Body.Instructions.Add(Instruction.Create(OpCodes.Ldarg_0)); trampoline.Body.Instructions.Add(Instruction.Create(OpCodes.Call, compiledPopulateMethod)); trampoline.Body.Instructions.Add(Instruction.Create(OpCodes.Ret)); @@ -243,7 +247,7 @@ namespace Avalonia.Build.Tasks i.Add(Instruction.Create(OpCodes.Newobj, parameterlessConstructor)); else { - i.Add(Instruction.Create(OpCodes.Ldnull)); + i.Add(Instruction.Create(OpCodes.Call, getRootServiceProvider)); i.Add(Instruction.Create(OpCodes.Call, compiledBuildMethod)); } diff --git a/src/Markup/Avalonia.Markup.Xaml/MarkupExtensions/StaticResourceExtension.cs b/src/Markup/Avalonia.Markup.Xaml/MarkupExtensions/StaticResourceExtension.cs index ac9a23d907..ea913db598 100644 --- a/src/Markup/Avalonia.Markup.Xaml/MarkupExtensions/StaticResourceExtension.cs +++ b/src/Markup/Avalonia.Markup.Xaml/MarkupExtensions/StaticResourceExtension.cs @@ -37,7 +37,8 @@ namespace Avalonia.Markup.Xaml.MarkupExtensions // We override XamlType.CanAssignTo in BindingXamlType so the results we get back // from GetAllAmbientValues aren't necessarily of the correct type. - if (resourceProvider is IControl control && control.StylingParent != null) + if (AvaloniaXamlLoader.UseLegacyXamlLoader + && resourceProvider is IControl control && control.StylingParent != null) { // If we've got to a control that has a StylingParent then it's probably // a top level control and its StylingParent is pointing to the global diff --git a/src/Markup/Avalonia.Markup.Xaml/XamlIl/AvaloniaXamlIlRuntimeCompiler.cs b/src/Markup/Avalonia.Markup.Xaml/XamlIl/AvaloniaXamlIlRuntimeCompiler.cs index 9c9418458a..24f8acc26c 100644 --- a/src/Markup/Avalonia.Markup.Xaml/XamlIl/AvaloniaXamlIlRuntimeCompiler.cs +++ b/src/Markup/Avalonia.Markup.Xaml/XamlIl/AvaloniaXamlIlRuntimeCompiler.cs @@ -8,6 +8,7 @@ using System.Reflection; using System.Reflection.Emit; using System.Runtime.InteropServices; using Avalonia.Markup.Xaml.XamlIl.CompilerExtensions; +using Avalonia.Markup.Xaml.XamlIl.Runtime; using Avalonia.Platform; using XamlIl.Transform; using XamlIl.TypeSystem; @@ -54,8 +55,7 @@ namespace Avalonia.Markup.Xaml.XamlIl _sreTypeSystem = new SreTypeSystem(); if (_sreBuilder == null) { - _sreCanSave = AvaloniaLocator.Current.GetService()?.GetRuntimeInfo().IsCoreClr == - false; + _sreCanSave = !(RuntimeInformation.FrameworkDescription.StartsWith(".NET Core")); var name = new AssemblyName(Guid.NewGuid().ToString("N")); if (_sreCanSave) { @@ -142,7 +142,7 @@ namespace Avalonia.Markup.Xaml.XamlIl var createCb = Expression.Lambda>( Expression.Convert(Expression.Call( created.GetMethod(AvaloniaXamlIlCompiler.BuildName), isp), typeof(object)), isp).Compile(); - return createCb(null); + return createCb(XamlIlRuntimeHelpers.GetRootServiceProviderV1()); } else { @@ -152,7 +152,7 @@ namespace Avalonia.Markup.Xaml.XamlIl var populateCb = Expression.Lambda>( Expression.Call(populate, isp, Expression.Convert(epar, populate.GetParameters()[1].ParameterType)), isp, epar).Compile(); - populateCb(null, rootInstance); + populateCb(XamlIlRuntimeHelpers.GetRootServiceProviderV1(), rootInstance); return rootInstance; } } diff --git a/src/Markup/Avalonia.Markup.Xaml/XamlIl/Runtime/XamlIlRuntimeHelpers.cs b/src/Markup/Avalonia.Markup.Xaml/XamlIl/Runtime/XamlIlRuntimeHelpers.cs index 53047ca902..2b4bff2059 100644 --- a/src/Markup/Avalonia.Markup.Xaml/XamlIl/Runtime/XamlIlRuntimeHelpers.cs +++ b/src/Markup/Avalonia.Markup.Xaml/XamlIl/Runtime/XamlIlRuntimeHelpers.cs @@ -124,5 +124,29 @@ namespace Avalonia.Markup.Xaml.XamlIl.Runtime string.Join(",", lst.Select(e => $"`{e.ClrAssemblyName}:{e.ClrNamespace}.{name}`"))); } } + + public static IServiceProvider GetRootServiceProviderV1() + { + return new RootServiceProvider(); + } + + class RootServiceProvider : IServiceProvider, IAvaloniaXamlIlParentStackProvider + { + public object GetService(Type serviceType) + { + if (serviceType == typeof(IAvaloniaXamlIlParentStackProvider)) + return this; + return null; + } + + public IEnumerable Parents + { + get + { + if (Application.Current != null) + yield return Application.Current; + } + } + } } }