Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
TemplatedButton.cs
1using System.Windows.Input;
4
6{
10 public class TemplatedButton : ContentView, IButtonElement
11 {
15 public TemplatedButton() :
16 base()
17 {
18 TapGestureRecognizer TapRecognizer = new();
19 TapRecognizer.Tapped += this.OnTapped;
20 this.GestureRecognizers.Add(TapRecognizer);
21 }
22
23 readonly WeakEventManager onClickedEventManager = new();
24
28 public static readonly BindableProperty CommandProperty = ButtonElement.CommandProperty;
29
33 public static readonly BindableProperty CommandParameterProperty = ButtonElement.CommandParameterProperty;
34
41 public ICommand? Command
42 {
43 get => (ICommand?)this.GetValue(CommandProperty);
44 set => this.SetValue(CommandProperty, value);
45 }
46
51 public object? CommandParameter
52 {
53 get => this.GetValue(CommandParameterProperty);
54 set => this.SetValue(CommandParameterProperty, value);
55 }
56
57 void ICommandElement.CanExecuteChanged(object? sender, EventArgs e) => this.RefreshIsEnabledProperty();
58
59 void IButtonElement.PropagateUpClicked() => this.onClickedEventManager.HandleEvent(this, EventArgs.Empty, nameof(Clicked));
60
61 protected override bool IsEnabledCore => base.IsEnabledCore && CommandElement.GetCanExecute(this);
62
66 public event EventHandler Clicked
67 {
68 add => this.onClickedEventManager.AddEventHandler(value);
69 remove => this.onClickedEventManager.RemoveEventHandler(value);
70 }
71
72 private async void OnTapped(object? Sender, EventArgs e)
73 {
74 try
75 {
76 // Change visual state to "Pressed"
77 VisualStateManager.GoToState(this, ButtonElement.PressedVisualState);
78
79 // Initialize TaskCompletionSource to await animation
80 TaskCompletionSource<bool> AnimationTask = new();
81
82 // Start the animation
83 this.Animate("Blink", new Animation()
84 {
85 { 0, 0.5, new((value) => this.Scale = value, 1, 0.95, Easing.CubicOut) },
86 { 0.5, 1, new((value) => this.Scale = value, 0.95, 1, Easing.CubicOut) },
87 },
88 finished: (value, isCompleted) => AnimationTask.SetResult(isCompleted));
89
90 // Await the animation's completion
91 //await AnimationTask.Task;
92
93 VisualStateManager.GoToState(this, VisualStateManager.CommonStates.Normal);
94
95 ButtonElement.ElementClicked(this, this);
96 }
97 catch (Exception ex)
98 {
99 ServiceRef.LogService.LogException(ex);
100 }
101 }
102 }
103}
Base class that references services in the app.
Definition: ServiceRef.cs:43
static ILogService LogService
Log service.
Definition: ServiceRef.cs:214
TemplatedButton represents a generalization of a Button whose appearance is defined by a ControlTempl...
TemplatedButton()
Initializes a new instance of TemplatedButton class.
EventHandler Clicked
Occurs when the button is clicked/tapped.
ICommand? Command
Gets or sets the command to invoke when the button is activated. This is a bindable property.
object? CommandParameter
Gets or sets the parameter to pass to the Command property. The default value is null....
static readonly BindableProperty CommandProperty
The backing store for the Command bindable property.
static readonly BindableProperty CommandParameterProperty
The backing store for the CommandParameter bindable property.