Advertisement
Guest User

Untitled

a guest
Mar 25th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.78 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Windows.Input;
  5. using PosApp.Components.Animations;
  6. using Xamarin.Forms;
  7.  
  8. namespace Apptivity.Components.Behaviors
  9. {
  10. public class FlexLayoutItemTappedBehavior : Behavior<FlexLayout>
  11. {
  12. public static readonly BindableProperty CommandProperty =
  13. BindableProperty.Create(nameof(Command), typeof(ICommand), typeof(FlexLayoutItemTappedBehavior), defaultBindingMode: BindingMode.OneWay);
  14.  
  15. public ICommand Command
  16. {
  17. get => (ICommand)this.GetValue(CommandProperty);
  18. set => this.SetValue(CommandProperty, value);
  19. }
  20.  
  21. protected override void OnAttachedTo(FlexLayout bindable)
  22. {
  23. base.OnAttachedTo(bindable);
  24.  
  25. if (bindable.BindingContext != null)
  26. {
  27. this.BindingContext = bindable.BindingContext;
  28. }
  29.  
  30. bindable.BindingContextChanged += this.OnFlexLayoutBindingChanged;
  31. bindable.ChildAdded += this.OnFlexLayoutChildAdded;
  32. }
  33.  
  34. protected override void OnDetachingFrom(FlexLayout bindable)
  35. {
  36. base.OnDetachingFrom(bindable);
  37.  
  38. bindable.BindingContextChanged -= this.OnFlexLayoutBindingChanged;
  39. bindable.ChildAdded -= this.OnFlexLayoutChildAdded;
  40.  
  41. foreach (var child in bindable.Children)
  42. {
  43. if (child is View childView && childView.GestureRecognizers.Any())
  44. {
  45. var tappedGestureRecognizers = childView.GestureRecognizers.Where(x => x is TapGestureRecognizer).Cast<TapGestureRecognizer>();
  46. foreach (var tapGestureRecognizer in tappedGestureRecognizers)
  47. {
  48. tapGestureRecognizer.Tapped -= this.OnItemTapped;
  49. childView.GestureRecognizers.Remove(tapGestureRecognizer);
  50. }
  51. }
  52. }
  53. }
  54.  
  55. private void OnFlexLayoutBindingChanged(object sender, EventArgs e)
  56. {
  57. if (sender is FlexLayout flexLayout)
  58. {
  59. this.BindingContext = flexLayout.BindingContext;
  60. }
  61. }
  62.  
  63. private void OnFlexLayoutChildAdded(object sender, ElementEventArgs args)
  64. {
  65. if (args.Element is View view)
  66. {
  67. var tappedGestureRecognizer = new TapGestureRecognizer();
  68. tappedGestureRecognizer.Tapped += this.OnItemTapped;
  69.  
  70. view.GestureRecognizers.Add(tappedGestureRecognizer);
  71. }
  72. }
  73.  
  74. private async void OnItemTapped(object sender, EventArgs e)
  75. {
  76. if (sender is VisualElement visualElement)
  77. {
  78. var animations = new List<AnimationBase>();
  79. var scaleIn = new ScaleToAnimation
  80. {
  81. Target = visualElement,
  82. ScaleTo = .95,
  83. Duration = 50
  84. };
  85. animations.Add(scaleIn);
  86.  
  87. var scaleOut = new ScaleToAnimation
  88. {
  89. Target = visualElement,
  90. ScaleTo = 1,
  91. Duration = 50
  92. };
  93. animations.Add(scaleOut);
  94.  
  95. var storyBoard = new StoryBoard(animations);
  96. await storyBoard.Begin();
  97. }
  98.  
  99. if (sender is BindableObject bindable && this.Command != null && this.Command.CanExecute(null))
  100. {
  101. this.Command.Execute(bindable.BindingContext);
  102. }
  103. }
  104. }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement