gpt4 book ai didi

c# - 在 Xamarin 中,如何仅更改按钮的背景颜色 1 秒钟,然后再将其恢复为原始颜色?

转载 作者:行者123 更新时间:2023-12-04 02:34:29 24 4
gpt4 key购买 nike

我正在 Xamarin 中开发一个小应用程序,我想在用户按下按钮时更改按钮的背景颜色仅持续 1 秒。我尝试使用的代码如下:

var auxColor = btnCancel.BackgroundColor;    // saving the original color (btnCancel is the button name)
btnCancel.BackgroundColor = Color.Red; // changing the color to red
Task.Delay(1000).Wait(); // waiting 1 second
btnCancel.BackgroundColor = auxColor; // restoring the original color

但我得到的是以下序列:

1-保存原始颜色
2- 等待 1 秒
3 将颜色更改为红色
4 立即恢复颜色

有人知道如何解决这个问题吗?

最佳答案

您可以按以下方式使用 Device.StartTimer

在您的按钮点击事件中:

private void OnButtonClicked(object sender, EventArgs e)
{
var auxColor = btnCancel.BackgroundColor; // saving the original color
btnCancel.BackgroundColor = Color.Red;

Device.StartTimer(TimeSpan.FromSeconds(1), () =>
{
btnCancel.BackgroundColor = auxColor;
return false;
});
}

要与 UI 元素交互,您可以使用 BeginInvokeOnMainThread,例如:

Device.StartTimer (new TimeSpan (0, 0, 1), () =>
{
// do something every 1 second
Device.BeginInvokeOnMainThread (() =>
{
// interact with UI elements
});
return true; // runs again, or false to stop
});

参见 Device.StartTimer文档。

关于c# - 在 Xamarin 中,如何仅更改按钮的背景颜色 1 秒钟,然后再将其恢复为原始颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62484199/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com