用于多个控件的Xamarin Forms Animation并行

时间:2019-03-18 14:15:21

标签: xamarin animation

对于我的 Xamarin Forms 项目,我需要同时对2个 Xamarin Frame 项目进行动画处理,但我只是不理解their example site中的示例使用

async void OnAnimateLabelButtonClicked(object sender, EventArgs e)
{
...
    await Task.WhenAll(
        label.ColorTo(Color.Red, Color.Blue, c => label.TextColor = c, 5000),
        label.ColorTo(Color.Blue, Color.Red, c => label.BackgroundColor = c, 5000));
...
}

Task.WhenAll -我不敢相信-无法保证它在GUI主线程上运行,因为没有 Device.BeginInvokeOnMainThread()

根据this论坛条目,一次使用多个动画的首选方式是通过

Animation a = new Animation();
a.Add(0, 1, new Animation(f => this.GuidanceLabel.Opacity = f, 1, 0, Easing.SinInOut, null));
a.Add(0, 1, new Animation(f => this.EnableAccess.Opacity = f, 1, 0, Easing.SinOut, null));
a.Commit(
        owner:this.GuidanceLabel,
        name:"DoubleFader",
        length:400,
        finished:(x, y) => {
                this.SetPhotoAccessDeniedState();
                this.GuidanceLabel.FadeTo(1, 400, Easing.CubicIn);
     }
);

所以,我想,我必须像上面那样将动画包装在一个 Device.BeginInvokeOnMainThread()使其正常工作, 对于我的特殊动画案例,工作流程为

Frame1.TranslateYto (-90, duration1);
Frame1.Content.IsVisible = true; // Was formerly false
Frame1.TranslateYto (0, duration2);

并同时在detto中用于 Frame2

所以我尝试过

Device.BeginInvokeOnMainThread(() =>
{
        Animation a = new Animation();
        a.Add(0, 1, new Animation(v => frame1.RotationY = v, 0, -90));
        a.Add(0, 1, new Animation(v => frame2.RotationY = v, 0, -90));
        a.Commit(
                owner: frame1,
                name: "flip1",
                length: 50,
                finished: (x, y) =>
                {
                        frame1.Content.IsVisible = false;
                        frame2.Content.IsVisible = false;
                });

        a = new Animation();
        a.Add(0, 1, new Animation(v => frame1.RotationY = v, -90, 0));
        a.Add(0, 1, new Animation(v => frame2.RotationY = v, -90, 0));
        a.Commit(
                owner: frame1,
                name: "flip2",
                length: 250);
});

但是由于 NullReferenceExecption ...

而崩溃

也许有人可以进一步阐明这个问题? 谢谢,

1 个答案:

答案 0 :(得分:1)

我在窗口中添加了两个Frame控件,并创建了与您相同的动画,但是没有任何问题。这是我的代码。

 <StackLayout>
        <Frame
            x:Name="frame1"
            HorizontalOptions="CenterAndExpand"
            VerticalOptions="FillAndExpand">
            <Label
                HorizontalOptions="CenterAndExpand"
                Text="this is test!!!!!!!"
                VerticalOptions="CenterAndExpand" />
        </Frame>

        <Frame x:Name="frame2" HorizontalOptions="CenterAndExpand" VerticalOptions="EndAndExpand">
            <Label
                HorizontalOptions="CenterAndExpand"
                Text="this is test22222222222222!!!!!!!"
                VerticalOptions="CenterAndExpand" />
        </Frame>

        <Button
            x:Name="btn1"
            Clicked="btn1_Clicked"
            HeightRequest="50"
            Text="btn1"
            WidthRequest="300" />
    </StackLayout>

 private void btn1_Clicked(object sender, EventArgs e)
    {
        Device.BeginInvokeOnMainThread(() => {
            Animation a = new Animation();
            a.Add(0, 1, new Animation(v => frame1.RotationY = v, 0, -90));
            a.Add(0, 1, new Animation(v => frame2.RotationY = v, 0, -90));
            a.Commit(
                    owner: frame1,
                    name: "flip1",
                    length: 50,
                    finished: (x, y) =>
                    {
                        frame1.Content.IsVisible = false;
                        frame2.Content.IsVisible = false;
                    });

            a = new Animation();
            a.Add(0, 1, new Animation(v => frame1.RotationY = v, -90, 0));
            a.Add(0, 1, new Animation(v => frame2.RotationY = v, -90, 0));
            a.Commit(
                    owner: frame1,
                    name: "flip2",
                    length: 250);

        });
    }

enter image description here