[ad_1]
I'm new to xamarin, I want to change color of a button on click after it should return back to default color once the process is completed, See the code below How can re render the button at run time.The command was executing first so I have handled it in click.This feature should work for android and iOS.
public class RippleButton : Button
private readonly Color _defaultBackgroundColor = Color.FromRgb(255, 87, 34);
private readonly Color _clickedBackgroundColor = Color.FromRgb(76, 175, 80);
public ICommand ClickCommand
get return (ICommand)GetValue(ClickCommandProperty);
set
SetValue(ClickCommandProperty, value);
public static readonly BindableProperty ClickCommandProperty = BindableProperty.Create(
propertyName: nameof(ClickCommand),
returnType: typeof(ICommand),
declaringType: typeof(RippleButton),
defaultValue: null,
defaultBindingMode: BindingMode.TwoWay,
propertyChanged: OnClickCommandChanged);
private static void OnClickCommandChanged(BindableObject bindable, object oldvalue, object newvalue)
public RippleButton()
const int animationTime = 10;
TextColor = Color.FromRgb(255, 255, 255);
BackgroundColor = _defaultBackgroundColor;
Clicked += async (sender, e) =>
var btn = (RippleButton)sender;
BackgroundColor = Color.FromRgb(76, 175, 80);
ClickCommand?.Execute(btn.CommandParameter);
await btn.ScaleTo(1.2, animationTime);
await btn.ScaleTo(1, animationTime);
BackgroundColor = _defaultBackgroundColor;
;
private void ChangeColorOfButton()
BackgroundColor = _clickedBackgroundColor;
Device.StartTimer(TimeSpan.FromSeconds(0.25), () =>
BackgroundColor = _defaultBackgroundColor;
return false;
);
[ad_2]
لینک منبع