Bright Lee
Bright Lee

Reputation: 2316

Image is not removed from AbsoluteLayout's Children on Xamarin.forms

I'm making app with using Xamarin.forms PCL project.

I made my timer and call this function every three seconds.

    void CreateBGAndAnimate()
    {
        Image bg = new Image()
        {
            Source = "_pagebaby_bg.png",
            Aspect = Aspect.Fill
        };
        AbsoluteLayout.SetLayoutFlags(bg, AbsoluteLayoutFlags.All);
        AbsoluteLayout.SetLayoutBounds(bg, new Rectangle(0.5, 1, 1, 0.5));

        xAbsoluteLayout.Children.Insert(1, bg);
        bg.Opacity = 0;

        Task.Factory.StartNew(async () =>
        {
            await bg.FadeTo(1, 500);
            await bg.ScaleTo(2, 1000, Easing.SinInOut);
            xAbsoluteLayout.Children.Remove(bg);
        });
    }

And very weird problem. FIrst Image never be removed. And Image created after first one are removed as I expected.

Is there someone facing same issue?

What did i wrong?

Thanks.

Upvotes: 0

Views: 125

Answers (2)

Rohit Vipin Mathews
Rohit Vipin Mathews

Reputation: 11787

You have do your UI operation in the UI thread (Main Thread). For that use the Device.BeginInvokeOnMainThread method.

void CreateBGAndAnimate()
{
    Image bg = new Image()
    {
        Source = "_pagebaby_bg.png",
        Aspect = Aspect.Fill
    };
    AbsoluteLayout.SetLayoutFlags(bg, AbsoluteLayoutFlags.All);
    AbsoluteLayout.SetLayoutBounds(bg, new Rectangle(0.5, 1, 1, 0.5));

    xAbsoluteLayout.Children.Insert(1, bg);
    bg.Opacity = 0;

    Task.Factory.StartNew(async () =>
    {
        await bg.FadeTo(1, 500);
        await bg.ScaleTo(2, 1000, Easing.SinInOut);

        Device.BeginInvokeOnMainThread(() => xAbsoluteLayout.Children.Remove(bg));
    });
}

Upvotes: 1

hvaughan3
hvaughan3

Reputation: 11105

Not sure but I would definitely make sure that the operations are running on the UI Thread and I switched around the Opacity and adding the control since you do not want the control to be visible for a split second after adding it in.

I also inserted a pause:

...

bg.Opacity = 0;
xAbsoluteLayout.Children.Insert(1, bg);

Xamarin.Forms.Device.BeginInvokeOnMainThread(async () =>
{
    await bg.FadeTo(1, 500);
    await bg.ScaleTo(2, 1000, Easing.SinInOut);
    xAbsoluteLayout.Children.Remove(bg);
});

Upvotes: 1

Related Questions