我有问题。我试图为我的背景色添加褪色效果,所以我创建了以下代码:
public GetStartedPage()
{
InitializeComponent();
new Task(() => { Animation(); }).Start();
}
private async void Animation()
{
while (true)
{
await this.ColorTo(Color.FromHex("#212121"), Color.FromHex("#5e5e5e"), c => this.BackgroundColor = c, 3000);
await this.ColorTo(Color.FromHex("#5e5e5e"), Color.FromHex("#212121"), c => this.BackgroundColor = c, 3000);
}
}
具有此扩展名:
public static class ViewExtensions
{
public static Task<bool> ColorTo(this VisualElement self, Color fromColor, Color toColor, Action<Color> callback, uint length = 250, Easing easing = null)
{
Func<double, Color> transform = (t) =>
Color.FromRgba(fromColor.R + t * (toColor.R - fromColor.R),
fromColor.G + t * (toColor.G - fromColor.G),
fromColor.B + t * (toColor.B - fromColor.B),
fromColor.A + t * (toColor.A - fromColor.A));
return ColorAnimation(self, "ColorTo", transform, callback, length, easing);
}
public static void CancelAnimation(this VisualElement self)
{
self.AbortAnimation("ColorTo");
}
static Task<bool> ColorAnimation(VisualElement element, string name, Func<double, Color> transform, Action<Color> callback, uint length, Easing easing)
{
easing = easing ?? Easing.Linear;
var taskCompletionSource = new TaskCompletionSource<bool>();
element.Animate<Color>(name, transform, callback, 16, length, easing, (v, c) => taskCompletionSource.SetResult(c));
return taskCompletionSource.Task;
}
}
当我这样做时,屏幕开始闪烁,但没有平滑的褪色。...
这是为什么,我该如何解决?