using System; using System.Collections.Generic; using Xamarin.Forms; namespace Aurora.Design.Components.ImageButton { public partial class ImageButton : ContentView { public ImageButton() { InitializeComponent(); } public static readonly BindableProperty SourceProperty = BindableProperty.Create( "Source", typeof(ImageSource), typeof(ImageButton), null, BindingMode.TwoWay, propertyChanged: (bindable, oldValue, newValue) => { ImageButton control = (ImageButton)bindable; control.imgButton.Source = (ImageSource)newValue; }); public static readonly BindableProperty CommandProperty = BindableProperty.Create( "Command", typeof(Command), typeof(ImageButton), null, propertyChanged: (bindable, oldValue, newValue) => { ImageButton control = (ImageButton)bindable; var command = (Command)newValue; CanExecute(command, control); command.CanExecuteChanged += (sender, e) => { CanExecute(sender, control); }; }); public static readonly BindableProperty CommandParameterProperty = BindableProperty.Create( "CommandParameter", typeof(object), typeof(ImageButton), null); private static void CanExecute(object sender, ImageButton control) { var cmd = (Command)sender; control.imgButton.IsEnabled = cmd.CanExecute(null); } public ImageSource Source { get { return (ImageSource)GetValue(SourceProperty); } set { SetValue(SourceProperty, value); } } public event EventHandler Tapped; public Command Command { get { return (Command)GetValue(CommandProperty); } set { SetValue(CommandProperty, value); } } public object CommandParameter { get { return GetValue(CommandParameterProperty); } set { SetValue(CommandParameterProperty, value); } } protected void OnButtonTapped(object sender, EventArgs args) { object resolvedParameter; if (CommandParameter != null) { resolvedParameter = CommandParameter; } else { resolvedParameter = args; } if (Command?.CanExecute(resolvedParameter) ?? true) { this.AbortAnimation("imgButtonAnim"); new Animation(v => imgButton.Scale = v, 1, 0.5).Commit(imgButton, "imgButtonAnim", 16, 150, Easing.SinOut, (v, c) => { imgButton.Scale = 1; Tapped?.Invoke(this, args); Command?.Execute(resolvedParameter); }); } } } }