Skip to content

Commit

Permalink
Allow dynamic XAML assembly to access controls
Browse files Browse the repository at this point in the history
  • Loading branch information
Kir-Antipov committed Dec 5, 2024
1 parent 6f72db7 commit 4edc9b8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/HotAvalonia/AvaloniaControlInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public 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, out compiledPopulateMethod);
control = AvaloniaControlHelper.Load(xaml, _uri, control, _controlType, out compiledPopulateMethod);
if (control is not null)
Refresh(control);

Expand Down
11 changes: 10 additions & 1 deletion src/HotAvalonia/Helpers/AvaloniaControlHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,22 +95,31 @@ private static void OnNewSreMethodBuilder(MethodBuilder methodBuilder, IEnumerab
assembly.AllowAccessTo(parameterType);
}

/// <inheritdoc cref="Load(string, Uri, object?, Type?, out MethodInfo?)"/>
public static object Load(string xaml, Uri uri, object? control, out MethodInfo? compiledPopulateMethod)
=> Load(xaml, uri, control, null, out compiledPopulateMethod);

/// <summary>
/// Loads an Avalonia control from XAML markup and initializes it.
/// </summary>
/// <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="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, out MethodInfo? compiledPopulateMethod)
public static object Load(string xaml, Uri uri, object? control, Type? controlType, out MethodInfo? compiledPopulateMethod)
{
_ = xaml ?? throw new ArgumentNullException(nameof(xaml));
_ = uri ?? throw new ArgumentNullException(nameof(uri));

controlType ??= control?.GetType();
if (controlType is not null && AvaloniaRuntimeXamlScanner.DynamicXamlAssembly is AssemblyBuilder xamlAssembly)
xamlAssembly.AllowAccessTo(controlType);

string xamlWithDynamicComponents = MakeStaticComponentsDynamic(xaml);
HashSet<MethodInfo> oldPopulateMethods = new(AvaloniaRuntimeXamlScanner.FindDynamicPopulateMethods(uri));

Expand Down

0 comments on commit 4edc9b8

Please sign in to comment.