Visual Studio / Xamarin OnClickListener
Visual Studio / Xamarin OnClickListener
我是编程新手,如果这是一个愚蠢的问题,我深表歉意!
我正在 VS15/Xamarin 中构建一个应用程序,并试图设置一个 onClickListener,但它一直告诉我有一个错误 "The type name 'OnClickListener' does not exist in the type 'View'"。
我尝试了多种解决方案,但显然我遗漏了一些东西!
这是我的代码:
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.Try 像这样的东西:
btn_About.Click += myCustomClick;
然后在你的oncreate之外:
public void myCustomClick(object o, EventArgs e) {
//handle click here
}
但是检查语法。
如果你想要它,你应该像这样制作你的 activity 实现 View.IOnClickListener
:
public class MainActivity: Activity, View.IOnClickListener
{
//code here
}
很久以前有人问过这个问题,但我找到了,其他人也可能找到了。希望这对某人有所帮助。
Xamarin 有它的新功能,使用 += EventHandler 有它的危险。首先,您必须确保在对象生命周期结束时注销此处理程序,否则会导致内存泄漏。使用 Androids ClickListener 确实是一个更好的解决方案。在 Xamarin 中,你可以做这样的事情
public class TEditClickListener : Java.Lang.Object, View.IOnClickListener
{
private RelayCommand _command;
public TEditClickListener(RelayCommand command)
{
_command = command;
}
public void OnClick(View v)
{
_command?.Execute(null);
}
}
然后实例化这个class并使用View.SetOnClickListener方法注册它。这样肯定不会有内存泄漏。
我是编程新手,如果这是一个愚蠢的问题,我深表歉意! 我正在 VS15/Xamarin 中构建一个应用程序,并试图设置一个 onClickListener,但它一直告诉我有一个错误 "The type name 'OnClickListener' does not exist in the type 'View'"。 我尝试了多种解决方案,但显然我遗漏了一些东西! 这是我的代码:
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.Try 像这样的东西:
btn_About.Click += myCustomClick;
然后在你的oncreate之外:
public void myCustomClick(object o, EventArgs e) {
//handle click here
}
但是检查语法。
如果你想要它,你应该像这样制作你的 activity 实现 View.IOnClickListener
:
public class MainActivity: Activity, View.IOnClickListener
{
//code here
}
很久以前有人问过这个问题,但我找到了,其他人也可能找到了。希望这对某人有所帮助。
Xamarin 有它的新功能,使用 += EventHandler 有它的危险。首先,您必须确保在对象生命周期结束时注销此处理程序,否则会导致内存泄漏。使用 Androids ClickListener 确实是一个更好的解决方案。在 Xamarin 中,你可以做这样的事情
public class TEditClickListener : Java.Lang.Object, View.IOnClickListener
{
private RelayCommand _command;
public TEditClickListener(RelayCommand command)
{
_command = command;
}
public void OnClick(View v)
{
_command?.Execute(null);
}
}
然后实例化这个class并使用View.SetOnClickListener方法注册它。这样肯定不会有内存泄漏。