xamarin-forms icon indicating copy to clipboard operation
xamarin-forms copied to clipboard

AnimationDemoPage throws exception when clicking on the button during animation

Open nicofr69 opened this issue 7 years ago • 0 comments

Hello David,

The AnimationDemoPage throws an exception when clicking on the button during animation: System.InvalidOperationException: An attempt was made to transition a task to a final state when it had already completed.

I found how to inactivate the button during the animation: <Button x:Name="runAnimationButton" Text="Run Animation"> <Button.Behaviors> <behaviors:EventHandlerBehavior EventName="Clicked"> <behaviors:SetPropertyAction TargetObject="{x:Reference runAnimationButton}" PropertyName="IsEnabled" Value="false" /> <behaviors:SetPropertyAction TargetObject="{x:Reference stopAnimationButton}" PropertyName="IsEnabled" Value="true" /> <behaviors:SetPropertyAction TargetObject="{x:Reference stopAnimationButton}" PropertyName="IsVisible" Value="true" /> ... <behaviors:SetPropertyAction TargetObject="{x:Reference runAnimationButton}" PropertyName="IsEnabled" Value="true" /> <behaviors:SetPropertyAction TargetObject="{x:Reference stopAnimationButton}" PropertyName="IsEnabled" Value="false" />

But I did not find how to cancel the animation by clicking on the "Stop animation" button. Here what I tried out: Above code for stopAnimationButton plus:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:behaviors="clr-namespace:Behaviors;assembly=Behaviors" x:Class="Behaviors.Sample.AnimationDemoPage" x:Name="animationPage" Title="Animation Demo" Icon="xaml.png">

    <Button x:Name="stopAnimationButton" Text="Stop animation (ineffective)" IsVisible="true" IsEnabled="false">
        <Button.Behaviors>
            <behaviors:EventHandlerBehavior EventName="Clicked">
                <behaviors:InvokeMethodAction TargetObject="{x:Reference animationPage}" MethodName="OnStopAnimation" />
                <behaviors:SetPropertyAction TargetObject="{x:Reference runAnimationButton}" PropertyName="IsEnabled" Value="true" />
            </behaviors:EventHandlerBehavior>
        </Button.Behaviors>
    </Button>

	public void **OnStopAnimation**(object sender, EventArgs args)
	{
		//KO no animation.Commit(.., "animationID", ...) defined: this.AbortAnimation("animationID";
		//KO still same runtime exception: ViewExtensions.**CancelAnimations**(image);
		ViewExtensions.**CancelAnimations**(this); //KO: no effect (but no exception neither)
	}

Any idea? Thanks.

Best regards, Nikolas

nicofr69 avatar Aug 31 '17 08:08 nicofr69