c# - 执行操作时为按钮设置动画 Xamarin Android
问题描述
我有一个按钮,它执行一个需要 3 秒的操作(从 API 读取数据并刷新屏幕)。执行操作时,我希望按钮旋转。我的问题是它在 3 秒操作完成后开始旋转......并且,它立即停止,因为它击中 ClearAnimation..
这是我的代码
this.refreshButton.Click += this.RefreshPendingOrders_Click;
private void RefreshPendingOrders_Click(object sender, EventArgs e)
{
this.StartRotateAnimation();
System.Threading.Thread.Sleep(3000);
this.refreshButton.ClearAnimation();
}
private void StartRotateAnimation()
{
var pivotX = this.refreshButton.Width / 2;
var pivotY = this.refreshButton.Height / 2;
var animation = new RotateAnimation(0f, 360f, pivotX, pivotY)
{
Duration = 500,
RepeatCount = 3
};
this.refreshButton.StartAnimation(animation);
}
解决方案
问题是您正在调用Thread.Sleep()
,如果 Click EventHandler 将在 UI 线程上发生。这意味着您正在阻塞负责运行动画 3 秒的 UI 线程。
您可以更改代码以使用 async/await 代替,例如:
private async void RefreshPendingOrders_Click(object sender, EventArgs e)
{
this.StartRotateAnimation();
await Task.Delay(3000);
this.refreshButton.ClearAnimation();
}
推荐阅读
- javascript - How do i check given duration times (00:20:40,1:20:40,00:00:10) is <20sec, >1hour and <10 seconds
- reactjs - 经常渲染 react-native 组件的好方法是什么?
- objective-c - 如何在 React 本机库中添加 Swift 代码?
- javascript - React:从嵌套的子组件更新父状态
- haskell - 从 Haskell 动态加载 .o 对象时函数解析出错
- php - 如何在 Laravel 中渴望加载 3 种不同的关系?
- c - Win32 (C) application freezes after some GetWindowText?
- android - Store json array keys data in separate arrayLists
- android - tns run android –bundle 失败,找不到元素
- python-3.x - Keras 序列模型