From d2100da9fab2b76e0e83c51b237265ca904f2469 Mon Sep 17 00:00:00 2001 From: Max Katz Date: Tue, 29 Aug 2023 09:10:24 +0000 Subject: [PATCH] Merge pull request #12047 from workgroupengineering/features/Core/StaticResourceExtensions feat: Handle ClrPropertyInfo in StaticResourceExtension --- .../MarkupExtensions/StaticResourceExtension.cs | 12 +++++++++--- .../Avalonia.Markup/Markup/Data/DelayedBinding.cs | 11 ++++++----- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/src/Markup/Avalonia.Markup.Xaml/MarkupExtensions/StaticResourceExtension.cs b/src/Markup/Avalonia.Markup.Xaml/MarkupExtensions/StaticResourceExtension.cs index c23c31e24c..acf1dc1143 100644 --- a/src/Markup/Avalonia.Markup.Xaml/MarkupExtensions/StaticResourceExtension.cs +++ b/src/Markup/Avalonia.Markup.Xaml/MarkupExtensions/StaticResourceExtension.cs @@ -32,14 +32,20 @@ namespace Avalonia.Markup.Xaml.MarkupExtensions var stack = serviceProvider.GetService(); var provideTarget = serviceProvider.GetService(); 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; diff --git a/src/Markup/Avalonia.Markup/Markup/Data/DelayedBinding.cs b/src/Markup/Avalonia.Markup/Markup/Data/DelayedBinding.cs index 6c70a81298..9c0153e7b9 100644 --- a/src/Markup/Avalonia.Markup/Markup/Data/DelayedBinding.cs +++ b/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 /// The control. /// The property on the control to bind to. /// A function which returns the value. - public static void Add(StyledElement target, PropertyInfo property, Func value) + public static void Add(StyledElement target, IPropertyInfo property, Func 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 value) + public ClrPropertyValueEntry(IPropertyInfo property, Func value) { Property = property; Value = value; } - public PropertyInfo Property { get; } + public IPropertyInfo Property { get; } public Func Value { get; } public override void Apply(StyledElement control) { try { - Property.SetValue(control, Value(control)); + Property.Set(control, Value(control)); } catch (Exception e) {