Xamarin表示动画并行处理多个控件

问题描述 投票:0回答:1

对于我的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);

并且对于并行的det2中的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 ...

也许有人可以对这个问题有所了解?谢谢,

xamarin animation
1个回答
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

© www.soinside.com 2019 - 2024. All rights reserved.