Visual Studio / Xamarin Onclicklistener
I'm new to programming, so I apologise if this is a stupid question! I'm building an app in VS15/Xamarin, and am trying to set an onClickListener, however it keep stelling me there
Solution 1:
C# is not java.Try something like this:
btn_About.Click += myCustomClick;
Then outside your oncreate:
publicvoidmyCustomClick(object o, EventArgs e) {
//handle click here
}
But check the syntax.
If you want it your way you should make your activity implement View.IOnClickListener
like this:
publicclassMainActivity: Activity, View.IOnClickListener
{
//code here
}
Solution 2:
Question was asked very long ago but I found it and so may others. Hope this helps somebody.
Xamarin has its newances and using += EventHandler has its dangers. Firstly you must be sure to unregister this handler at the end of the object's lifecycle as otherwise it will cause memory leaks. Using Androids ClickListener is really a better solution. In Xamarin you can do something like this
publicclassTEditClickListener : Java.Lang.Object, View.IOnClickListener
{
private RelayCommand _command;
publicTEditClickListener(RelayCommand command)
{
_command = command;
}
publicvoidOnClick(View v)
{
_command?.Execute(null);
}
}
and then instantiate this class and use View.SetOnClickListener method to register it. This way there surely won't be memory leaks.
Post a Comment for "Visual Studio / Xamarin Onclicklistener"