Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 .

like image 399
user3091970 Avatar asked Jan 30 '16 17:01

user3091970


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


2 Answers

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

like image 167
SushiHangover Avatar answered Sep 28 '22 10:09

SushiHangover


The real issue here is in your SetOnClickListener you are setting an inline anonymous class implementing OnClickListener interface.

This is not supported in C#, From the C# programming guide, you can find,

Anonymous types are class types that consist of one or more public read-only properties. No other kinds of class members such as methods or events are allowed. An anonymous type cannot be cast to any interface or type except for object.

But it doesn't mean that you cannot use SetOnClickListener at all.

You can either do button1.SetOnClickListener(this) and implement your OnClickListener in the same class

or

create a class (can be inner class) implements OnClickListener with your implementation and pass an it's instance to your SetOnClickListener

In both ways, your are obeying C#'s "Real Name Policy" :)

like image 26
Let'sRefactor Avatar answered Sep 28 '22 12:09

Let'sRefactor