Forum Xamarin.Forms

How to prevent pages from navigating Multiple Forms

In Xamarin Forms, I have a list view and on tapping one item in the list view it will be taking us to the next content view. If I am tapping the item in the list view quickly for multiple times we can see the screens are navigated multiple times. How can we avoid it?

Answers

  • NMackayNMackay GBInsider, University mod

    @FXamain

    This topic is covered extensively in forum posts but this approach should work for you.

    SomeCommand = new Command (OnCommand,(x)=> CanNavigate);
    
    async void OnCommand (object obj)
    {
            CanNavigate = false;
    
            await CurrentPage.DisplayAlert ("Hello", "From intelliAbb", "OK");
    
            CanNavigate = true;
    
    }
    

    Here's the original SO post

    https://stackoverflow.com/questions/35727228/disable-android-double-tap-on-xamarin-forms-label

    Check out the blog post as well, it's worth reading
    https://intelliabb.com/2017/02/18/handling-multiple-taps-in-xamarin-forms-on-android/

  • AshaarAshaar USMember ✭✭
    edited July 2019

    Try this.

     if (Application.Current.MainPage.Navigation.NavigationStack.Count == 0 || 
                    Application.Current.MainPage.Navigation.NavigationStack.Last().GetType() != typeof(ImageTypePage))
                {
                    await navigationService.PushTo(new ImageTypePage(keys, IoCContainer.Container.Resolve<ImageTypeViewModel>()));
                }  
    
  • ayberkzerayayberkzeray USMember
    edited February 11

    I use this in my NavigationService

                if (CurrentApplication.MainPage is NavigationPage navigationPage)
                                {
                                    if (_currentPage != null &&  _currentPage.GetType() == page.GetType())
                                    {
                                        return;
                                    }
                                    _currentPage = page;
                                    await navigationPage.PushAsync(page, animate);
    
                                  }
    
Sign In or Register to comment.