Forum Xamarin.Forms

How to prevent firing of event multiple times on rapid click of a button in Xamarin forms

Mars07Mars07 USMember ✭✭
edited November 2020 in Xamarin.Forms

How can I avoid invoking of the same event multiple times when a button is clicked rapidly.

Below is the code: I've created a Custom Delegate Command as below

View Model

namespace TestProject.ViewModels
{
    public class TestViewModel 
    {

        public CustomDelegateCommand MenuButtonClickCommand { get; set; }


        public TestViewModel()
        {
          MenuButtonClickCommand = new CustomDelegateCommand (async () => await ShowMenuAction());
        }


        private async Task ShowMenuAction()
        {
            //await some stuff
        }
    }
}

CustomDelegateCommand.cs

public class CustomDelegateTimerCommand : DelegateCommand
{
        public CustomDelegateTimerCommand(Action executeMethod, Func<bool> validateMethod, Action onBusy = default(Action)) : base(executeMethod)
        {
            BackgroundTaskWaitHandle = new EventWaitHandle(true, EventResetMode.ManualReset);
            _validateMethod = validateMethod;
            _onBusy = onBusy;
        }

}

The problem I'm facing is whenever a user clicks on the button rapidly, the menu list popup is opening multiple times. I have lot of commands in my project and I need a solution that would work globally.

I tried to resolve the issue like below using ObservesCanExecute() but I don't like the idea of creating a separate variable for every command as I've a lot of commands in my project and I don't want the button to go in to disabled state when CanExecute = false.

ViewModel

MenuButtonClickCommand = new CustomDelegateCommand (async () => await ShowMenuAction().ObservesCanExecute(() => CanExecute );

private async Task ShowMenuAction()
        {
            CanExecute = false;

            //await some stuff

            CanExecute = true;
        }

Any help is much appreciated!

Answers

  • YelinzhYelinzh Member, Xamarin Team Xamurai
    edited November 2020

    How can I avoid invoking of the same event multiple times when a button is clicked rapidly

    The OnDisappearing method of the page will be called when the popup is shown. You could create a global static bool property in the App class and change the value of the property in the OnDisappearing method .

    Then detect the property in the command method to prevent the function execution.

    Check the code:

    public partial class App : Application
    {
        public static bool IsPopupShown { get; set; }
        public App()
        {
            InitializeComponent();
            MainPage = new NavigationPage(new MainPage());
        }
        ...
    }
    

    Page.xaml.cs

    protected override void OnDisappearing()
    {
        base.OnAppearing();
    
        App.IsPopupShown = true;
    }
    

    Function method:

    private async Task ShowMenuAction()
    {
        if (App.IsPopupShown) return;
        ...
        App.IsPopupShown = false;
    }
    
  • YelinzhYelinzh Member, Xamarin Team Xamurai
    edited November 2020

    @Mars07 Hi, any updates? If the solution helps you to solve the issue, please accept the helpful reply as the answer. It'll help others who face the similar problem. If you are facing some issues while implementing, try to post the particular error with the corresponding codes here.


    Xamarin forums are migrating to a new home on Microsoft Q&A!
    We invite you to post new questions in the Xamarin forums’ new home on Microsoft Q&A!
    For more information, please refer to this sticky post.

Sign In or Register to comment.