How to know if a button has been clicked? Xamarin Forms

Asked

Viewed 232 times

1

I have sometimes needed to know when a button was clicked and did not find a property natively in C#/Xamarin. For example:

inserir a descrição da imagem aqui

In this image above we have a Entry with a Text (for example only) written Pesquisa... by Defaut and with a function ToUpper (to capitalize all text), in case I click the button LIMPAR I wish I could add the text Pesquisa... again, but the function ToUpper forces the text to stay PESQUISA.... So I thought I’d make one if who did the following:

if (BotãoLimparPressionado = true)
{
   EntryPesquisar = "Pesquisa..."
}
else    //a função para os outros casos continuariam normais
{
   EntryPesquisar.ToUpper
}
  • But if I’m on the current button putting this if doesn’t make any sense.

  • 1

    Sorry, it’s just that I found the question self-explanatory, now I tried to improve, I edited it.

1 answer

2


Assuming you are developing the visual via XAML...

In your XAML use

<Button Clicked="OnClick">Button</Button>

And in your codebehind

private bool BotaoClicado = false;

public void OnClick(object sender, EventArgs args)
{
  BotaoClicado = true;
  // Aqui dentro você poem sua programação
}

private void OutroMetodo()
{
  if(BotaoClicado)
  {
    //Aqui você poem o que deve ser feito
  }
}
  • I’d be at another event calling one if to know if the button was clicked or not, see my example in the question asked.

  • 1

    I got it wrong, I’m sorry! For this, your own question answers itself, to know if the button has already been clicked you must create a variable for this control, check the editing of my answer.

  • As everything indicates the variable would work, but does not have a native property of C#/Xamarin that would take this click information? Know?

  • I believe there is no other way, if there is something native I don’t know

  • Awn, right, thank you so.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.