Arvind Chourasiya
Arvind Chourasiya

Reputation: 17472

How to use SetCustomAnimations on page transition from cross platform project

When pushing and popping the pages I am using Custom renderer for left and right transition. I want to write that code in shared project without using custom renderer or dependency services

This is my code. How can I write this totally cross platform

    if
    {
        transaction.SetCustomAnimations(Resource.Animation.enter_right, Resource.Animation.exit_left,
            Resource.Animation.enter_left, Resource.Animation.exit_right);
    }
    else
    {
        transaction.SetCustomAnimations(Resource.Animation.enter_left, Resource.Animation.exit_right,
            Resource.Animation.enter_right, Resource.Animation.exit_left);
    }

Upvotes: 0

Views: 165

Answers (1)

Leon Lu
Leon Lu

Reputation: 9274

You can use Xamarin.Plugin.SharedTransitions this plugin.

https://github.com/GiampaoloGabba/Xamarin.Plugin.SharedTransitions

You can use it like following step.

1.In the App.xaml.cs, use following code to warp the MainPage.

  public App()
        {
            InitializeComponent();
            var sharedTransitionNavigationPage=new SharedTransitionNavigationPage(new MainPage());

            MainPage = sharedTransitionNavigationPage;
           
        }

In the MainPage.xam.cs add following code for Animations and navigation.

namespace MyPopUpPageDemo
{
    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            //for Animations
            SharedTransitionNavigationPage.SetBackgroundAnimation(this, BackgroundAnimation.SlideFromRight);
            SharedTransitionNavigationPage.SetTransitionDuration(this, 2000);
            InitializeComponent();
        }

        private void Button_Clicked(object sender, EventArgs e)
        {
            //for navigation
            Navigation.PushAsync(new Page1());

        }

     
    }
}

In the page1, that is same.

 [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class Page1 : ContentPage
    {
        public Page1()
        {
            SharedTransitionNavigationPage.SetBackgroundAnimation(this, BackgroundAnimation.SlideFromRight);
            SharedTransitionNavigationPage.SetTransitionDuration(this, 1000);

            InitializeComponent();
        }

        private void Button_Clicked(object sender, EventArgs e)
        {
            Navigation.PushAsync(new Page2());
        }
    }

Here is running GIF.

enter image description here

Upvotes: 1

Related Questions