Get control name in Button event handler method Xamarin Forms

前端 未结 3 725
不知归路
不知归路 2021-01-05 04:47

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

相关标签:
3条回答
  • 2021-01-05 05:22

    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;
            }
    
    0 讨论(0)
  • 2021-01-05 05:26

    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)){
       }
    }
    
    0 讨论(0)
  • 2021-01-05 05:40
    var btn = (Button)sender;
    
    if(btn.Id == btn1.Id) {
    
    }
    else if(btn.Id == btn2.Id){
    }
    
    0 讨论(0)
提交回复
热议问题