AvaloniaPropertyAccessorPlugin.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. using System;
  2. using System.Diagnostics.CodeAnalysis;
  3. using System.Runtime.ExceptionServices;
  4. using Avalonia.Utilities;
  5. namespace Avalonia.Data.Core.Plugins
  6. {
  7. /// <summary>
  8. /// Reads a property from a <see cref="AvaloniaObject"/>.
  9. /// </summary>
  10. public class AvaloniaPropertyAccessorPlugin : IPropertyAccessorPlugin
  11. {
  12. /// <inheritdoc/>
  13. [RequiresUnreferencedCode(TrimmingMessages.PropertyAccessorsRequiresUnreferencedCodeMessage)]
  14. public bool Match(object obj, string propertyName)
  15. {
  16. if (obj is AvaloniaObject o)
  17. {
  18. return LookupProperty(o, propertyName) != null;
  19. }
  20. return false;
  21. }
  22. /// <summary>
  23. /// Starts monitoring the value of a property on an object.
  24. /// </summary>
  25. /// <param name="reference">A weak reference to the object.</param>
  26. /// <param name="propertyName">The property name.</param>
  27. /// <returns>
  28. /// An <see cref="IPropertyAccessor"/> interface through which future interactions with the
  29. /// property will be made.
  30. /// </returns>
  31. [RequiresUnreferencedCode(TrimmingMessages.PropertyAccessorsRequiresUnreferencedCodeMessage)]
  32. public IPropertyAccessor? Start(WeakReference<object?> reference, string propertyName)
  33. {
  34. _ = reference ?? throw new ArgumentNullException(nameof(reference));
  35. _ = propertyName ?? throw new ArgumentNullException(nameof(propertyName));
  36. if (!reference.TryGetTarget(out var instance) || instance is null)
  37. return null;
  38. var o = (AvaloniaObject)instance;
  39. var p = LookupProperty(o, propertyName);
  40. if (p != null)
  41. {
  42. return new Accessor(new WeakReference<AvaloniaObject>(o), p);
  43. }
  44. else if (instance != AvaloniaProperty.UnsetValue)
  45. {
  46. var message = $"Could not find AvaloniaProperty '{propertyName}' on '{instance}'";
  47. var exception = new MissingMemberException(message);
  48. return new PropertyError(new BindingNotification(exception, BindingErrorType.Error));
  49. }
  50. else
  51. {
  52. return null;
  53. }
  54. }
  55. private static AvaloniaProperty? LookupProperty(AvaloniaObject o, string propertyName)
  56. {
  57. return AvaloniaPropertyRegistry.Instance.FindRegistered(o, propertyName);
  58. }
  59. private class Accessor : PropertyAccessorBase, IWeakEventSubscriber<AvaloniaPropertyChangedEventArgs>
  60. {
  61. private readonly WeakReference<AvaloniaObject> _reference;
  62. private readonly AvaloniaProperty _property;
  63. public Accessor(WeakReference<AvaloniaObject> reference, AvaloniaProperty property)
  64. {
  65. _reference = reference ?? throw new ArgumentNullException(nameof(reference));
  66. _property = property ?? throw new ArgumentNullException(nameof(property));
  67. }
  68. public AvaloniaObject? Instance
  69. {
  70. get
  71. {
  72. _reference.TryGetTarget(out var result);
  73. return result;
  74. }
  75. }
  76. public override Type? PropertyType => _property?.PropertyType;
  77. public override object? Value => Instance?.GetValue(_property);
  78. public override bool SetValue(object? value, BindingPriority priority)
  79. {
  80. if (!_property.IsReadOnly)
  81. {
  82. Instance?.SetValue(_property, value, priority);
  83. return true;
  84. }
  85. return false;
  86. }
  87. void IWeakEventSubscriber<AvaloniaPropertyChangedEventArgs>.
  88. OnEvent(object? notifyPropertyChanged, WeakEvent ev, AvaloniaPropertyChangedEventArgs e)
  89. {
  90. if (e.Property == _property)
  91. {
  92. SendCurrentValue();
  93. }
  94. }
  95. protected override void SubscribeCore()
  96. {
  97. SubscribeToChanges();
  98. SendCurrentValue();
  99. }
  100. protected override void UnsubscribeCore()
  101. {
  102. var instance = Instance;
  103. if (instance != null)
  104. WeakEvents.AvaloniaPropertyChanged.Unsubscribe(instance, this);
  105. }
  106. private void SendCurrentValue()
  107. {
  108. try
  109. {
  110. var value = Value;
  111. PublishValue(value);
  112. }
  113. catch { }
  114. }
  115. private void SubscribeToChanges()
  116. {
  117. var instance = Instance;
  118. if (instance != null)
  119. WeakEvents.AvaloniaPropertyChanged.Subscribe(instance, this);
  120. }
  121. }
  122. }
  123. }