From 703287e8d0abb69491b446fe112503f5f55265bb Mon Sep 17 00:00:00 2001 From: Kir_Antipov Date: Mon, 16 Dec 2024 21:34:03 +0300 Subject: [PATCH] Supply `AvaloniaRuntimeXamlLoader` with assembly Fixes #21 --- src/HotAvalonia/AvaloniaControlInfo.cs | 2 +- src/HotAvalonia/Helpers/AvaloniaControlHelper.cs | 13 +++++++------ 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/HotAvalonia/AvaloniaControlInfo.cs b/src/HotAvalonia/AvaloniaControlInfo.cs index ef961f7..fda2dd3 100644 --- a/src/HotAvalonia/AvaloniaControlInfo.cs +++ b/src/HotAvalonia/AvaloniaControlInfo.cs @@ -139,7 +139,7 @@ internal AvaloniaControlInfo( /// The newly compiled populate method, if the compilation was successful. public object? Load(string xaml, object? control, out MethodInfo? compiledPopulateMethod) { - control = AvaloniaControlHelper.Load(xaml, _uri, control, _controlType, out compiledPopulateMethod); + control = AvaloniaControlHelper.Load(xaml, _uri, control, _build.DeclaringType?.Assembly, out compiledPopulateMethod); if (control is not null) Refresh(control); diff --git a/src/HotAvalonia/Helpers/AvaloniaControlHelper.cs b/src/HotAvalonia/Helpers/AvaloniaControlHelper.cs index 0f5fb00..a74ceaf 100644 --- a/src/HotAvalonia/Helpers/AvaloniaControlHelper.cs +++ b/src/HotAvalonia/Helpers/AvaloniaControlHelper.cs @@ -5,6 +5,7 @@ using Avalonia.LogicalTree; using Avalonia.Markup.Xaml; using Avalonia.Markup.Xaml.XamlIl.Runtime; +using Avalonia.Platform; using Avalonia.Styling; using HotAvalonia.Reflection.Inject; @@ -44,13 +45,13 @@ public static object Load(string xaml, Uri uri, object? control, out MethodInfo? /// The XAML markup to load the control from. /// The URI that identifies the XAML source. /// The optional control object to be populated. - /// The type of the control. + /// The assembly defining the control. /// The newly compiled populate method, if the compilation was successful. /// An object representing the loaded Avalonia control. /// /// This method replaces static resources with their dynamic counterparts before loading the control. /// - public static object Load(string xaml, Uri uri, object? control, Type? controlType, out MethodInfo? compiledPopulateMethod) + public static object Load(string xaml, Uri uri, object? control, Assembly? assembly, out MethodInfo? compiledPopulateMethod) { _ = xaml ?? throw new ArgumentNullException(nameof(xaml)); _ = uri ?? throw new ArgumentNullException(nameof(uri)); @@ -60,15 +61,15 @@ public static object Load(string xaml, Uri uri, object? control, Type? controlTy // which are commonly used for subscribing to events (e.g., `Click`, // `TextChanged`, etc.). To circumvent this problem, we need to // patch the dynamic XAML assembly with `IgnoresAccessChecksToAttribute`. - controlType ??= control?.GetType(); - if (controlType?.Assembly is Assembly controlAssembly) - AvaloniaRuntimeXamlScanner.DynamicXamlAssembly?.AllowAccessTo(controlAssembly); + assembly ??= AssetLoader.GetAssembly(uri); + if (assembly is not null) + AvaloniaRuntimeXamlScanner.DynamicXamlAssembly?.AllowAccessTo(assembly); string xamlWithDynamicComponents = MakeStaticComponentsDynamic(xaml); HashSet oldPopulateMethods = new(AvaloniaRuntimeXamlScanner.FindDynamicPopulateMethods(uri)); Reset(control, out Action restore); - object loadedControl = AvaloniaRuntimeXamlLoader.Load(xamlWithDynamicComponents, null, control, uri, designMode: false); + object loadedControl = AvaloniaRuntimeXamlLoader.Load(xamlWithDynamicComponents, assembly, control, uri, designMode: false); restore(); compiledPopulateMethod = AvaloniaRuntimeXamlScanner