Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

245
Vistas
Get control name in Button event handler method Xamarin Forms

I have 20 buttons in my Xamarin Forms app . All of the buttons share the same click event method. What I want to do is use switch statement to check the button name but I am having difficulty finding the controls name that fired the event .

Please see the code :

private  void btnCollection_Clicked(object sender, EventArgs args)
    {
        var btn = (Button)sender;

        switch (btn.FindByName) // I want to get the name of the control 
        {

            case "btn1":
                break;

                case "btn2":
                break;
        }
    }

How can I get the button's name?

about 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

You cannot access the x:Name property of a xaml element as this is just a hint for the compiler to name the variable.

What you can do however is to set the ClassId of your button so that you can retrieve it in the handler. Like this: Your xaml :

<Button ClassId="sdsd"
              Clicked="Button_OnClicked"/>

Your xaml.cs

private void LoginButton_OnClicked(object sender, EventArgs e)
        {
            var button = (Button) sender;
            var classId = button.ClassId;
        }
about 4 years ago · Santiago Trujillo Denunciar

0

Maybe this can help you

if(sender is Button){

   Button button = (Button)sender;
   if(button.Equals(myButton1)){
      // You are in myButton1
   }else if(button.Equals(myButton2)){
   }
}
about 4 years ago · Santiago Trujillo Denunciar

0

var btn = (Button)sender;

if(btn.Id == btn1.Id) {

}
else if(btn.Id == btn2.Id){
}
about 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda