

Animating Page Transitions in Xamarin Forms
source link: https://xamgirl.com/animating-page-transitions-in-xamarin-forms/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

When developing mobile applications, having a good UI/UX is one of the most important things. One way we can help with our UX by having smooth transitions between pages. Xamarin Forms uses the default standard transition navigation:
But what if you want to go further? And do some of those cool transition animations that the community is doing?
Achieving this is easy when using the awesome NuGet package by Giampaolo Gabba Xamarin.Plugin.SharedTransitions.
Let’s do it step by step
The animation that is shown above is the result of the example we are going to do Step by Step. It has two pages (HomePage and DetailPage), when selecting an item from the HomePage list, it will navigate to the DetailPage.
1. Install the NuGet package SharedTransitions in each of our Xamarin projects.
2. Use SharedTransitionNavigationPage for Navigation
When navigating, instead of using the default NavigationPage you will use this one:
await Navigation.PushAsync(new CustomTransitionNavPage(new HomePage()));
Prism Ex:
If you are using Prism you must register the SharedTransitionNavigationPage, and use it when navigating:
namespace SharedTransitionPrimSample { public partial class App : PrismApplication { protected override void OnInitialized() { InitializeComponent(); NavigationService.NavigateAsync("SharedTransitionNavigationPage/HomePage"); }
protected override void RegisterTypes(IContainerRegistry containerRegistry) { containerRegistry.RegisterForNavigation<SharedTransitionNavigationPage>(); containerRegistry.RegisterForNavigation<HomePage, HomePageViewModel>(); } } }
NOTE: If you check my code on Github you will see that I created a subclass of SharedTransitionNavigationPage (CustomTransitionNavPage) and registered it and used it instead of using the SharedTransitionNavigationPage that’s because I wanted to add some extra logic to make the NavigationBar translucent.
3. Specify the Animation/Duration/Group on the page from where you navigate.
On the page from where you navigate (HomePage) you must specify three main attached properties:
- TransitionSelectedGroup: Unique group used to associate transition groups in dynamic lists. (It can be the Id of the element selected)
- TransitionDuration: Duration in milliseconds of the shared transition.
- BackgroundAnimation: It supports 6 types of animations (Fade, Flip, SlideFromLeft, SlideFromRight, SlideFromTop and SlideFromBottom)
<?xml version="1.0" encoding="UTF-8"?> <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="SharedTransitionPrimSample.Views.HomePage" xmlns:sharedTransitions="clr-namespace:Plugin.SharedTransitions;assembly=Plugin.SharedTransitions" sharedTransitions:SharedTransitionNavigationPage.TransitionSelectedGroup="{Binding SelectedPlaceId}" sharedTransitions:SharedTransitionNavigationPage.TransitionDuration="300" sharedTransitions:SharedTransitionNavigationPage.BackgroundAnimation="Flip"/>
4. Add the name and the group to the element you want to animate in the page from where you navigate (HomePage).
You must specify two main properties:
- Transition.Name: The name of the transition used to associate views between pages.
- Transition.Group: A unique identifier of the item (If you are using a list, it can be the id)
<Image Source="{Binding Image}" sharedTransitions:Transition.Name="PlaceImage" sharedTransitions:Transition.Group="{Binding Id}" Aspect="AspectFill"/>
Check the full view Here.
5. Add the Navigation to the page you are navigating to and set the TransitionSelectedGroup property.
Before Navigating make sure to set the TransitionSelectedGroup (Specified in Step 3).
namespace SharedTransitionPrimSample.ViewModels { public class HomePageViewModel : BindableBase { public DelegateCommand<Place> NavigateToDetailCommand { get; }
private string _selectedPlaceId; public string SelectedPlaceId { get => _selectedPlaceId; set => SetProperty(ref _selectedPlaceId, value); }
public HomePageViewModel(INavigationService navigationService) { NavigateToDetailCommand = new DelegateCommand<Place>(async(param) => await ExecuteNavigateToDetail(param)); ... }
async Task ExecuteNavigateToDetail(Place place) { SelectedPlaceId = place.Id; await _navigationService.NavigateAsync("DetailPage", (("PLACE", place))); } } }
Since I’m using Prism and I’m doing the Navigation in the ViewModel I have a SelectedPlaceId property to set the value when selecting an item of the list, but if you are not using any framework, you can do it in the code behind and set the value directly.
Check the full ViewModel here.
5. On the DetailPage add the same id used in Step 4 as TransitionName to the same element type
<Image Source="{Binding Image}" sharedTransitions:Transition.Name="PlaceImage" Aspect="AspectFill" HeightRequest="400"/>
Full Detail Page here and Full ViewModel here.
IMPORTANT: For this example, I also added some logic to make the NavigationPage translucent, therefore you don’t see the Navigation Bar in the git. If you want this same behavior here is the extra code I added:
Result with different Transition Types
Fade
SlideFromBottom
SlideFromLeft
SlideFromRight
SlideFromTop
Flip
For more information on how to use the NuGet package, limitations, etc. Check the library documentation.
That’s all for now, you can find the full source code used here.
Happy coding!
Like this:
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK