16

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?

2
  • btn.Name should work... Your btn reference shows to your Button so you can directly request the name from it. Commented Mar 17, 2017 at 13:14
  • 1
    There is no Name property in xamarin forms buttons ! Commented Mar 17, 2017 at 13:24

4 Answers 4

37

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;
        }
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks a lot Daniel . It works .Sorry I can't vote for the answer you gave because I don't have enough reputation yet.
This solves the issue but as per Microsoft at this link, this it not ClassId's purpose. I too am stuck at this point (new to Xamarin). So in need of a real solution. I exactly have the same problem, want to identify the button uniquely.
You can create a class that inherits from Button in which you have whatever you want and set it from xaml
11

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)){
   }
}

Comments

3
var btn = (Button)sender;

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

}
else if(btn.Id == btn2.Id){
}

3 Comments

Please add a little explain about how this fix is solving the issue or, what was the incorrect part in the code posted in the question.
And what is _Rubrica?
Pardon .. it's an xaml button : <Button x:Name="_Rubrica"
1

I'm sure @abdul is far past this now, but ClassId should be used to identify a type of button.

I would expect each button to have a unique text field, though:

<Button Text="My awesome button" Clicked="MyAwesomeClickHandler"/>

Which may be easily read in the event handler method:

private void MyAwesomeClickHandler(object sender, EventArgs e)
{
     Button myAwesomeButton= (Button) sender;
     string myAwesomeText= myAwesomeButton.Text;
}

This is how most people I know do it, so I thought it might help.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.