user3091970 user3091970 - 1 month ago 27
Android Question

C# Xamarin OnClick function

What I'm doing is this

Button button1 = FindViewById<Button>(Resource.Id.button1);

button1.SetOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
// Perform action on click
}
});


but for some reasons I'm getting
OnClickListener
underlined with red. and I can't do anything to launch a function when I click my button .

Answer

The Xamarin.Android way of doing a SetOnClickListener is via C# style events:

Button button1 = FindViewById<Button>(Resource.Id.button1);
button1.Click += (sender, e) => {
   // Perform action on click
};

Required reading for Xamarin's Android Events and Listeners