How to set onclick listener in xamarin?

Artūrs Eimanis picture Artūrs Eimanis · Feb 4, 2016 · Viewed 26.4k times · Source

I'm quite new to C# and Xamarin and have been trying to implement a bottom sheet element and don't know how to correctly do it. I am using Cocosw.BottomSheet-Xamarin.Android library.

Here is my code:

Cocosw.BottomSheetActions.BottomSheet.Builder b = new Cocosw.BottomSheetActions.BottomSheet.Builder (this);
b.Title ("New");
b.Sheet (Resource.Layout.menu_bottom_sheet)

Now i think i should use b.Listener(...), but it requires an interface IDialogInterfaceOnClickListener as a paramater and i don't know how to do it in C# correctly.

In Java i could write

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

I tried doing this:

class BottomSheetActions : IDialogInterfaceOnClickListener {
    public void OnClick (IDialogInterface dialog, int which) {
        Console.WriteLine ("Hello fox");
    }

    public IntPtr Handle { get; }

    public void Dispose() {

    }
}

and then this:

b.Listener (new BottomSheetActions());

But it didnt work.

Answer

Let'sRefactor picture Let'sRefactor · Feb 4, 2016

Use click event instead.

button.Click += delegate 
{
    //Your code
};

See my other answer for more info