PointerEventsBehavior.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. using Avalonia.Input;
  2. using Avalonia.Interactivity;
  3. namespace Avalonia.Xaml.Interactions.Events;
  4. /// <summary>
  5. ///
  6. /// </summary>
  7. public abstract class PointerEventsBehavior : InteractiveBehaviorBase
  8. {
  9. static PointerEventsBehavior()
  10. {
  11. RoutingStrategiesProperty.OverrideMetadata<PointerEventsBehavior>(
  12. new StyledPropertyMetadata<RoutingStrategies>(
  13. defaultValue: RoutingStrategies.Tunnel | RoutingStrategies.Bubble));
  14. }
  15. /// <inheritdoc />
  16. protected override void OnAttachedToVisualTree()
  17. {
  18. if (AssociatedObject is not null)
  19. {
  20. AssociatedObject.AddHandler(InputElement.PointerPressedEvent, PointerPressed, RoutingStrategies);
  21. AssociatedObject.AddHandler(InputElement.PointerReleasedEvent, PointerReleased, RoutingStrategies);
  22. AssociatedObject.AddHandler(InputElement.PointerMovedEvent, PointerMoved, RoutingStrategies);
  23. }
  24. }
  25. /// <inheritdoc />
  26. protected override void OnDetachedFromVisualTree()
  27. {
  28. if (AssociatedObject is not null)
  29. {
  30. AssociatedObject.RemoveHandler(InputElement.PointerPressedEvent, PointerPressed);
  31. AssociatedObject.RemoveHandler(InputElement.PointerReleasedEvent, PointerReleased);
  32. AssociatedObject.RemoveHandler(InputElement.PointerMovedEvent, PointerMoved);
  33. }
  34. }
  35. private void PointerPressed(object? sender, PointerPressedEventArgs e)
  36. {
  37. OnPointerPressed(sender, e);
  38. }
  39. private void PointerReleased(object? sender, PointerReleasedEventArgs e)
  40. {
  41. OnPointerReleased(sender, e);
  42. }
  43. private void PointerMoved(object? sender, PointerEventArgs e)
  44. {
  45. OnPointerMoved(sender, e);
  46. }
  47. /// <summary>
  48. ///
  49. /// </summary>
  50. /// <param name="sender"></param>
  51. /// <param name="e"></param>
  52. protected virtual void OnPointerPressed(object? sender, PointerPressedEventArgs e)
  53. {
  54. }
  55. /// <summary>
  56. ///
  57. /// </summary>
  58. /// <param name="sender"></param>
  59. /// <param name="e"></param>
  60. protected virtual void OnPointerReleased(object? sender, PointerReleasedEventArgs e)
  61. {
  62. }
  63. /// <summary>
  64. ///
  65. /// </summary>
  66. /// <param name="sender"></param>
  67. /// <param name="e"></param>
  68. protected virtual void OnPointerMoved(object? sender, PointerEventArgs e)
  69. {
  70. }
  71. }