Visual Studio / Xamarin OnClickListener

不羁的心 提交于 2019-12-11 12:35:32

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!