【发布时间】:2017-09-02 09:39:16
【问题描述】:
我是 xamarin 的新手,我想在单击后更改按钮的颜色,一旦该过程完成,它应该返回默认颜色,请参阅下面的代码如何在运行时重新呈现按钮。命令是先执行的,所以我在 click 中处理了它。这个功能应该适用于 android 和 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;
});
}
}
【问题讨论】:
-
你的意思是在你按下按钮的时候改变按钮的颜色,在你松开按钮的时候改变它的颜色?
-
是的。一旦该过程完成,它应该返回到默认颜色。
标签: c# xamarin mvvm xamarin.forms