user12945091
user12945091

Reputation:

Xamarin Forms Animation MVVM way

I want to make an animation that gets fired when a frame is clicked. I already have a command that's bound on the GestureRecognizers but that's part of my ViewModel and putting view code inside the ViewModel breaks the mvvm. Do you know where the animation code should be placed?(i want to perform the animation and after that to execute the command from the ViewModel)

Upvotes: 0

Views: 424

Answers (1)

Cfun
Cfun

Reputation: 9721

You can run the animation in the code-behind and at the end call your ViewModel method (the action currently executed by the bound command), no need for the Command in this case, only the event.

xaml

<Image Source="tapped.jpg">
    <Image.GestureRecognizers>
        <TapGestureRecognizer
                Tapped="OnTapGestureRecognizerTapped"/>
  </Image.GestureRecognizers>
</Image>

Code-behind

async void OnTapGestureRecognizerTapped(object sender, EventArgs args)
{
     //await the animation
     (BindingContext as YourViewModel).CommandAction();
}

ViewModel

void CommandAction()
{
...
}

Upvotes: 1

Related Questions