1
私はxamarinを初めて使いました。プロセスが完了したらデフォルトの色に戻す必要があるので、クリック時にボタンの色を変更したいです。下のコードを参照してください。実行時間。コマンドが最初に実行されたので、私はclick.Thisの機能でこれを処理していたアンドロイドとiOSで動作するはずです。Xamarinフォームのボタンの色を変更する
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;
});
}
}
あなたがボタンを押している間、ボタンの色を変更し、あなたがそれを離すと戻ってそれを変更することを意味していますか? –
はい。プロセスが完了したら、デフォルトの色に戻ります。 –