Button clicked multiple times

ArmandCharbonnierArmandCharbonnier FRMember ✭✭
edited July 2016 in Xamarin.Forms

Hi everyone,

I noticed a common problem in my applications in XF. When i have a simple button with a command to navigate to a new page, if i click quickly twice on that button, it opens 2 pages. I tried with the CanExecute and a IsBusy property, but the problem remains.

Is there a way to handle that kind of behavior ? Or maybe block the click during the transition.

Thanks,

Best regards,

Armand

PS: For information, I mostly tried on Android.

Posts

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    I use a global property App.IsPressed

    In Clicked event I write something like

    try {
        If (App.IsPressed)
            return;
    
        App.IsPressed = true
    
        // Do something...
    
        App.IsPressed = false;
    }
    catch(Excecption ex){
        App.IsPressed = false;
    }
    

    someone use a Timer...

  • NMackayNMackay GBInsider, University mod
    edited July 2016

    @ArmandCharbonnier

    As you can make the command async you can set a flag (I set the current page in my viewmodellocator) but essentially doing the same as Alessandro suggested.

     public RelayCommand<AppFavourite> SelectFavCommand
            {
                get
                {
                    return _selectFavCommand
                           ?? (_selectFavCommand = new RelayCommand<AppFavourite>(
                               async fav =>
                               {
                                   try
                                   {
                                       if (!Connectivity.IsDataConnectionEnabled())
                                           return;
    
                                       // Check for double tap if already awaiting nav
                                       if (_navService.CurrentPageKey == ViewModelLocator.PageKeyContactDetails) return;
    
            // This sets the current page key and as it's awaitable multiple calls will be ignored with the line above
                                       await _navService.NavigateTo(ViewModelLocator.PageKeyContactDetails, fav,false);
                                   }
                                   catch (Exception ex)
                                   {
                                       _exService.Error(GetType().Name, ex);
                                   }
                               },
                               fav => fav != null));
                }
            }
    
  • ArmandCharbonnierArmandCharbonnier FRMember ✭✭

    @AlessandroCaliaro, @NMackay,

    Thanks for you answers, i'll try this today and see if it works in my project :)

    Best regards,

    Armand

  • ArmandCharbonnierArmandCharbonnier FRMember ✭✭
    edited July 2016

    Thanks both of you, it works :)

Sign In or Register to comment.