Browse Source

Application is now always the root of the parent stack

pull/2322/head
Nikita Tsukanov 7 years ago
parent
commit
d01d69fbef
  1. 8
      src/Avalonia.Build.Tasks/XamlCompilerTaskExecutor.cs
  2. 3
      src/Markup/Avalonia.Markup.Xaml/MarkupExtensions/StaticResourceExtension.cs
  3. 8
      src/Markup/Avalonia.Markup.Xaml/XamlIl/AvaloniaXamlIlRuntimeCompiler.cs
  4. 24
      src/Markup/Avalonia.Markup.Xaml/XamlIl/Runtime/XamlIlRuntimeHelpers.cs

8
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));
}

3
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

8
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<IRuntimePlatform>()?.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<Func<IServiceProvider, object>>(
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<Action<IServiceProvider, object>>(
Expression.Call(populate, isp, Expression.Convert(epar, populate.GetParameters()[1].ParameterType)),
isp, epar).Compile();
populateCb(null, rootInstance);
populateCb(XamlIlRuntimeHelpers.GetRootServiceProviderV1(), rootInstance);
return rootInstance;
}
}

24
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<object> Parents
{
get
{
if (Application.Current != null)
yield return Application.Current;
}
}
}
}
}

Loading…
Cancel
Save