Visual Studio/Xamarin OnClickListener [英] Visual Studio / Xamarin OnClickListener

查看:29
本文介绍了Visual Studio/Xamarin OnClickListener的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是编程新手,如果这是一个愚蠢的问题,我深表歉意!我正在 VS15/Xamarin 中构建一个应用程序,并试图设置一个 onClickListener,但是它一直告诉我有一个错误类型名称‘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 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));
                }
            }
        }        
    }
}

画面如下:

截图

推荐答案

C# 不是 java.尝试这样的事情:

C# is not java.Try something like this:

btn_About.Click += myCustomClick;

然后在您的 oncreate 之外:

Then outside your oncreate:

public void myCustomClick(object o, EventArgs e) {
//handle click here
}

但请检查语法.

如果你想要它,你应该让你的 Activity 像这样实现 View.IOnClickListener:

If you want it your way you should make your activity implement View.IOnClickListener like this:

public class MainActivity: Activity, View.IOnClickListener
{
 //code here
}

这篇关于Visual Studio/Xamarin OnClickListener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆