“我目前正在开发一个 .NET MAUI 项目。假设用户可以使用同一个按钮开始和停止录制。我希望在录制时有连续的动画,并且当按钮停止时,动画应该此功能也有效,但是当我连续多次快速单击该按钮时,该应用程序崩溃并停止运行,我们将不胜感激。
private bool isRecording = false;
public MainPage()
{
InitializeComponent();
BindingContext = new MainViewModel();
}
public void StartStopButton_Clicked(object sender, EventArgs e)
{
isRecording = !isRecording;
if (isRecording)
{
AnimateButton(testButton);
}
else
{
...
}
private async Task AnimateButton(View view)
{
while (isRecording)
{
await view.ScaleTo(2, 200, Easing.SinInOut);
await view.ScaleTo(1, 200, Easing.SinInOut);
}
}
view.ScaleTo
当不是从 UI 线程调用时会崩溃。您可以同时进行多次调用。
bool isAnimating;
private async Task AnimateButton(View view)
{
if (isAnimating)
return;
isAnimating=true;
MainThread.BeginInvokeOnMainThread(() =>
{
while (isRecording)
{
await view.ScaleTo(2, 200, Easing.SinInOut);
await view.ScaleTo(1, 200, Easing.SinInOut);
}
isAnimating=false;
});
}