Skip to content

Commit

Permalink
Supply AvaloniaRuntimeXamlLoader with assembly
Browse files Browse the repository at this point in the history
Fixes #21
  • Loading branch information
Kir-Antipov committed Dec 16, 2024
1 parent 7a86eb3 commit 703287e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/HotAvalonia/AvaloniaControlInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ internal AvaloniaControlInfo(
/// <param name="compiledPopulateMethod">The newly compiled populate method, if the compilation was successful.</param>
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);

Expand Down
13 changes: 7 additions & 6 deletions src/HotAvalonia/Helpers/AvaloniaControlHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -44,13 +45,13 @@ public static object Load(string xaml, Uri uri, object? control, out MethodInfo?
/// <param name="xaml">The XAML markup to load the control from.</param>
/// <param name="uri">The URI that identifies the XAML source.</param>
/// <param name="control">The optional control object to be populated.</param>
/// <param name="controlType">The type of the control.</param>
/// <param name="assembly">The assembly defining the control.</param>
/// <param name="compiledPopulateMethod">The newly compiled populate method, if the compilation was successful.</param>
/// <returns>An object representing the loaded Avalonia control.</returns>
/// <remarks>
/// This method replaces static resources with their dynamic counterparts before loading the control.
/// </remarks>
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));
Expand All @@ -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<MethodInfo> 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
Expand Down

0 comments on commit 703287e

Please sign in to comment.