Stavrogin
Stavrogin

Reputation: 143

Disable Clicking/Tapping more than Once on Button/Label in Xamarin

In my Xamarin App, for Navigation, I made the ImageButton, Label e.t.c clickable as Button. But the problem is, that if someone clicks the Label, ImageButton, Button rapidly (more than once), the Page appears multiple time.

I want to prevent this behavior, user shouldn't be able to click more than once.

For Button, in ViewModel, I used the UserDialogs.Instance.Loading("wait") as well, but if user clicks rapidly, the button get clicked more than once before the UserDialogs Instance appears.

Label

<Label x:Name="label">
    <Label.GestureRecognizers>
        <TapGestureRecognizer Command="{Binding LabelCommand}"
                              NumberOfTapsRequired="1" />
    </Label.GestureRecognizers>
</Label>

Button

<Button x:Name="button"
        Command="{Binding ButtonCommand}" />

Upvotes: 1

Views: 1164

Answers (3)

Stavrogin
Stavrogin

Reputation: 143

Disable the Button/Label for 2 Seconds using await Task.Delay(), when it's clicked.

For Button

<Button x:Name="button"
        Command="{Binding ButtonCommand}"
        IsEnabled="True"
        Clicked="buttonClicked" />
public async void buttonClicked(object sender, EventArgs args)
{
    button.IsEnabled = false;
    await Task.Delay(2000);
    button.IsEnabled = true;
}

For Label using TapGestureRecognizer

<Label x:Name="label"
       IsEnabled="True">
    <Label.GestureRecognizers>
        <TapGestureRecognizer Command="{Binding LabelCommand}"
                              Clicked="labelClicked"
                              NumberOfTapsRequired="1" />
    </Label.GestureRecognizers>
</Label>
public async void labelClicked(object sender, EventArgs args)
{
    label.IsEnabled = false;
    await Task.Delay(2000);
    label.IsEnabled = true;
}

Upvotes: 4

Miamy
Miamy

Reputation: 2299

You could use the next snippet:

private bool wasPressed;

public void CommandAction(object parameter)
{
    if (wasPressed)
    {
        return;
    }
    wasPressed = true;
    try
    {
         // ... your code
    }
    finally
    {
        wasPressed = false;
    }
}

Upvotes: 2

Tomasz Cekało
Tomasz Cekało

Reputation: 41

How about disabling the button when clicked? And enabling when UserDialog is closed?

Upvotes: 3

Related Questions