Browse Source

Merge pull request #12047 from workgroupengineering/features/Core/StaticResourceExtensions

feat: Handle ClrPropertyInfo in StaticResourceExtension
release/11.0.5-rc1
Max Katz 3 years ago
committed by Steven Kirk
parent
commit
d2100da9fa
  1. 12
      src/Markup/Avalonia.Markup.Xaml/MarkupExtensions/StaticResourceExtension.cs
  2. 11
      src/Markup/Avalonia.Markup/Markup/Data/DelayedBinding.cs

12
src/Markup/Avalonia.Markup.Xaml/MarkupExtensions/StaticResourceExtension.cs

@ -32,14 +32,20 @@ namespace Avalonia.Markup.Xaml.MarkupExtensions
var stack = serviceProvider.GetService<IAvaloniaXamlIlParentStackProvider>();
var provideTarget = serviceProvider.GetService<IProvideValueTarget>();
var targetObject = provideTarget?.TargetObject;
var targetProperty = provideTarget?.TargetProperty;
var targetProperty = provideTarget?.TargetProperty switch
{
AvaloniaProperty ap => ap,
PropertyInfo pi => new Avalonia.Data.Core.ReflectionClrPropertyInfo(pi),
_ => provideTarget?.TargetProperty,
};
var themeVariant = (targetObject as IThemeVariantHost)?.ActualThemeVariant
?? GetDictionaryVariant(serviceProvider);
var targetType = targetProperty switch
{
AvaloniaProperty ap => ap.PropertyType,
PropertyInfo pi => pi.PropertyType,
Avalonia.Data.Core.IPropertyInfo cpi => cpi.PropertyType,
_ => null
};
@ -62,7 +68,7 @@ namespace Avalonia.Markup.Xaml.MarkupExtensions
}
if (targetObject is Control target &&
targetProperty is PropertyInfo property)
targetProperty is Avalonia.Data.Core.IPropertyInfo property)
{
// This is stored locally to avoid allocating closure in the outer scope.
var localTargetType = targetType;

11
src/Markup/Avalonia.Markup/Markup/Data/DelayedBinding.cs

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Reflection;
using System.Runtime.CompilerServices;
using Avalonia.Data;
using Avalonia.Data.Core;
using Avalonia.Logging;
namespace Avalonia.Markup.Data
@ -56,11 +57,11 @@ namespace Avalonia.Markup.Data
/// <param name="target">The control.</param>
/// <param name="property">The property on the control to bind to.</param>
/// <param name="value">A function which returns the value.</param>
public static void Add(StyledElement target, PropertyInfo property, Func<StyledElement, object?> value)
public static void Add(StyledElement target, IPropertyInfo property, Func<StyledElement, object?> value)
{
if (target.IsInitialized)
{
property.SetValue(target, value(target));
property.Set(target, value(target));
}
else
{
@ -125,20 +126,20 @@ namespace Avalonia.Markup.Data
private class ClrPropertyValueEntry : Entry
{
public ClrPropertyValueEntry(PropertyInfo property, Func<StyledElement, object?> value)
public ClrPropertyValueEntry(IPropertyInfo property, Func<StyledElement, object?> value)
{
Property = property;
Value = value;
}
public PropertyInfo Property { get; }
public IPropertyInfo Property { get; }
public Func<StyledElement, object?> Value { get; }
public override void Apply(StyledElement control)
{
try
{
Property.SetValue(control, Value(control));
Property.Set(control, Value(control));
}
catch (Exception e)
{

Loading…
Cancel
Save