问题
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 is an error "The type name 'OnClickListener' does not exist in the type 'View'". I've tried a number of solutions, but clearly I'm missing something! This is my code:
using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
namespace MyStory
{
[Activity(Label = "MyStory", MainLauncher = true, Icon = "@drawable/Books")]
public class MainActivity : Activity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
Button btn_Short = FindViewById<Button>(Resource.Id.btn_Short);
Button btn_Flash = FindViewById<Button>(Resource.Id.btn_Flash);
Button btn_Poet = FindViewById<Button>(Resource.Id.btn_Poet);
Button btn_About = FindViewById<Button>(Resource.Id.btn_About);
btn_About.SetOnClickListener(new View.OnclickListener())
{
@override
public void onClick(View v)
{
startActivity(new Intent(MainActivity.this, About.class));
}
}
}
}
}
This is what the screen looks like:
screenshot
回答1:
C# is not java.Try something like this:
btn_About.Click += myCustomClick;
Then outside your oncreate:
public void myCustomClick(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:
public class MainActivity: Activity, View.IOnClickListener
{
//code here
}
来源:https://stackoverflow.com/questions/36855674/visual-studio-xamarin-onclicklistener