首页 > 解决方案 > Xamarin 闪屏

问题描述

我有个问题。我正在尝试为我的背景颜色添加褪色效果,因此我创建了以下代码:

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;
    }
}

当我这样做时,屏幕开始闪烁,但没有平滑褪色......

为什么会这样,我该如何解决?

标签: c#xamarinxamarin.formsxamarin.androidxamarin.ios

解决方案


推荐阅读