Xamarin表单动画不重复

时间:2016-11-30 14:45:24

标签: animation xamarin xamarin.forms

我正在尝试在Xamarin.Forms(版本2.3.3.168)中为图像设置动画。动画正在运行,但不会重复。

public class WelcomePage : ContentPage
{
    Image img;

    public WelcomePage()
    {
        img = new Image
        {
            Source = "img.png",
            HorizontalOptions = LayoutOptions.Center,
            VerticalOptions = LayoutOptions.Center,
        };

        Content = new StackLayout
        {
            VerticalOptions = LayoutOptions.Center,
            Children = {
                img
            }
        };
    }

    protected override void OnAppearing()
    {
        base.OnAppearing();

        var a = new Animation();
        a.Add(0, 0.5, new Animation((v) =>
        {
            img.Scale = v;
        }, 1.0, 1.2, Easing.CubicInOut, () => { System.Diagnostics.Debug.WriteLine("ANIMATION A"); }));
        a.Add(0.5, 1, new Animation((v) =>
        {
            img.Scale = v;
        }, 1.2, 1.0, Easing.CubicInOut, () => { System.Diagnostics.Debug.WriteLine("ANIMATION B"); }));
        a.Commit(img, "animation", 16, 2000, Easing.Linear, (d, f) => img.Scale = 1.0, () =>
        {
            System.Diagnostics.Debug.WriteLine("ANIMATION ALL");
            return true;
        });
    }
}

运行应用程序几秒钟后,将打印以下调试输出:

ANIMATION A
ANIMATION B
ANIMATION ALL
ANIMATION ALL
ANIMATION ALL

我正在测试这是一个UWP。

1 个答案:

答案 0 :(得分:2)

根据this thread on the Xamarin forums,其他人似乎也有同样的问题。它似乎与每个子动画中设置的私有属性有关。

解决方法是每次链完成时重新创建动画链:

public class WelcomePage : ContentPage
{
    Image img;

    public WelcomePage()
    {
        img = new Image
        {
            Source = "circle_plus.png",
            HorizontalOptions = LayoutOptions.Center,
            VerticalOptions = LayoutOptions.Center,
        };

        Content = new StackLayout
        {
            VerticalOptions = LayoutOptions.Center,
            Children = {
                img
            }
        };
    }

    protected override void OnAppearing()
    {
        base.OnAppearing();
        animate();
    }

    void animate()
    {
        var a = new Animation();
        a.Add(0, 0.5, new Animation((v) =>
        {
            img.Scale = v;
        }, 1.0, 1.2, Easing.CubicInOut, () => { System.Diagnostics.Debug.WriteLine("ANIMATION A"); }));
        a.Add(0.5, 1, new Animation((v) =>
        {
            img.Scale = v;
        }, 1.2, 1.0, Easing.CubicInOut, () => { System.Diagnostics.Debug.WriteLine("ANIMATION B"); }));
        a.Commit(img, "animation", 16, 2000, null, (d, f) =>
        {
            img.Scale = 1.0;
            System.Diagnostics.Debug.WriteLine("ANIMATION ALL");
            animate();
        });
    }
}