using System;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Linq;
using System.Reflection;
using Avalonia.Controls;
namespace Avalonia.Xaml.Interactions.Core;
///
/// An action that will change a specified property to a specified value when invoked.
///
[RequiresUnreferencedCode("This functionality is not compatible with trimming.")]
public class ChangePropertyAction : Avalonia.Xaml.Interactivity.Action
{
private static readonly char[] s_trimChars = ['(', ')'];
private static readonly char[] s_separator = ['.'];
[RequiresUnreferencedCode("This functionality is not compatible with trimming.")]
private static Type? GetTypeByName(string name)
{
return
AppDomain.CurrentDomain.GetAssemblies()
.Reverse()
.Select(assembly => assembly.GetType(name))
.FirstOrDefault(t => t is not null)
??
AppDomain.CurrentDomain.GetAssemblies()
.Reverse()
.SelectMany(assembly => assembly.GetTypes())
.FirstOrDefault(t => t.Name == name);
}
[RequiresUnreferencedCode("This functionality is not compatible with trimming.")]
private static AvaloniaProperty? FindAttachedProperty(object? targetObject, string propertyName)
{
if (targetObject is null)
{
return null;
}
var propertyNames = propertyName.Trim().Trim(s_trimChars).Split(s_separator);
if (propertyNames.Length != 2)
{
return null;
}
var targetPropertyTypeName = propertyNames[0];
var targetPropertyName = propertyNames[1];
var targetType = GetTypeByName(targetPropertyTypeName) ?? targetObject.GetType();
var registeredAttached = AvaloniaPropertyRegistry.Instance.GetRegisteredAttached(targetType);
foreach (var avaloniaProperty in registeredAttached)
{
if (avaloniaProperty.OwnerType.Name == targetPropertyTypeName && avaloniaProperty.Name == targetPropertyName)
{
return avaloniaProperty;
}
}
var registeredInherited = AvaloniaPropertyRegistry.Instance.GetRegisteredInherited(targetType);
foreach (var avaloniaProperty in registeredInherited)
{
if (avaloniaProperty.Name == targetPropertyName)
{
return avaloniaProperty;
}
}
return null;
}
///
/// Identifies the avalonia property.
///
public static readonly StyledProperty PropertyNameProperty =
AvaloniaProperty.Register(nameof(PropertyName));
///
/// Identifies the avalonia property.
///
public static readonly StyledProperty