markpirvine
markpirvine

Reputation: 1592

Xamarin.Forms Carousel Page Swipe Event Page Changing

I'm using the Carousel Page that ships with Xamarin Form (2.5.0) and would like to be able to detect when a page is about to change.

Each Page has some validation and if this fails I want to prevent the user from moving to the next page. Currently the only event I can see is PageChanged and this is where I trigger the validation, however the user must swipe back to see the error message.

Is there a way to hook into the native swipe event?

The App is currently targeting iOS, however it will eventually need to support Android.

Upvotes: 0

Views: 3587

Answers (1)

K232
K232

Reputation: 1069

I know it's a bit late, but maybe someone else might also ask, so here's an idea: As you wrote, in

protected override void OnCurrentPageChanged()    

you could find out when a user swiped to the next page. Now if you do the validation for the previous page, you could go back to the previous page in case of error with

int index = Children.IndexOf(CurrentPage);
this.CurrentPage = this.Children[index-1];

This does not prevent the user from leaving the page (as there is no 'OnSwipingStart' or else), but the user will see the error without having to move back manually.

If you are happy to use a different control than CarouselPage: There is a pull request to get CarouselView into native Xamarin Forms. It has a Scrolled-Event which is triggered already on the first percent swiped so you could do your actions there.

Upvotes: 3

Related Questions