如何在跨平台项目的页面过渡上使用SetCustomAnimations

时间:2020-11-03 09:44:32

标签: xamarin xamarin.forms

按下并弹出pages时,我使用Custom renderer进行左右转换。我想在共享项目中编写该代码而不使用custom rendererdependency services

这是我的代码。我怎么能完全写成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);
    }

1 个答案:

答案 0 :(得分:1)

您可以使用Xamarin.Plugin.SharedTransitions这个插件。

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

您可以按照以下步骤使用它。

1。在App.xaml.cs中,使用以下代码来扭曲主页。

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

            MainPage = sharedTransitionNavigationPage;
           
        }

MainPage.xam.cs中为Animations和导航添加以下代码。

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());

        }

     
    }
}

在page1中,是相同的。

 [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());
        }
    }

此处正在运行GIF。

enter image description here