Skip to content

Commit

Permalink
fix(hr): Use type mapping for x:Bind inner bindings class
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromelaban committed Oct 24, 2023
1 parent 71758d1 commit 8b44f26
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -983,7 +983,11 @@ private void BuildCompiledBindingsInitializer(IndentedStringBuilder writer, INam

if (hasXBindExpressions || hasResourceExtensions)
{
writer.AppendLineIndented($"Bindings = new {GetBindingsTypeNames(_xClassName.ClassName).bindingsClassName}(this);");
var activator = _isHotReloadEnabled
? $"(({GetBindingsTypeNames(_xClassName.ClassName).bindingsInterfaceName})global::Uno.UI.Helpers.TypeMappings.CreateInstance<{GetBindingsTypeNames(_xClassName.ClassName).bindingsClassName}>(this))"
: $"new {GetBindingsTypeNames(_xClassName.ClassName).bindingsClassName}(this)";

writer.AppendLineIndented($"Bindings = {activator};");
}

if (isFrameworkElement && (hasXBindExpressions || hasResourceExtensions))
Expand Down
9 changes: 9 additions & 0 deletions src/Uno.UI/Helpers/TypeMappings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,15 @@ public static Type GetReplacementType(this Type instanceType)
public static object CreateInstance<TOriginalType>()
=> Activator.CreateInstance(typeof(TOriginalType).GetReplacementType());

/// <summary>
/// Creates an instance of the replacement type using its original type.
/// </summary>
/// <typeparam name="TOriginalType">The original type to be created</typeparam>
/// <param name="args">The arguments used to create the instance, passed to the ctor</param>
/// <returns>An new instance for the original type</returns>
public static object CreateInstance<TOriginalType>(params object[] args)
=> Activator.CreateInstance(typeof(TOriginalType).GetReplacementType(), args: args);

internal static Type GetMappedType(this Type originalType) =>
OriginalTypeToMappedType.TryGetValue(originalType, out var mappedType) ? mappedType : default;

Expand Down

0 comments on commit 8b44f26

Please sign in to comment.