C# 按钮onClick未绑定到活动中的方法
因此,我正在尝试向我的主C# 按钮onClick未绑定到活动中的方法,c#,android,xamarin,C#,Android,Xamarin,因此,我正在尝试向我的主活动使用的.axml文件中的按钮添加一个click事件。我已经经历了很多其他线程,并且我已经尽我所能地遵循了提供的代码。但是,当我运行代码时,我会遇到以下异常 Java.Lang.IllegalStateException: Could not find method testClick(View) in a parent or ancestor Context for android:onClick attribute defined on view class a
活动使用的.axml文件中的按钮添加一个click事件。我已经经历了很多其他线程,并且我已经尽我所能地遵循了提供的代码。但是,当我运行代码时,我会遇到以下异常
Java.Lang.IllegalStateException: Could not find method testClick(View)
in a parent or ancestor Context for android:onClick attribute
defined on view class android.widget.Button with id 'login'
在Main.axml中
<Button
android:text="Login"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:onClick="testClick"
android:id="@+id/login" />
我是否缺少一些配置,或者是否包括使其正常工作的配置?您始终可以这样做:
Button button1 = FindViewById<Button>(Resource.Id. login);
button1.Click += delegate => {
//Show alert saying clicked...
};
如果这些都不起作用,请添加对Mono.Android.Export.dll
的引用,然后在Activity
中放入此项
[Java.Interop.Export("testClick")]
public void testClick(View view)
{
//Show alert saying clicked...
}
请在继承接口视图后重试。IOnclicklistener:
public class Main: AppcompatActivity, View.IonClickListener
它有助于实现接口的onclick方法,如下所示:
public void testClick(View view)
{
//Show alert saying clicked...
}
您是否构建了它或尝试重新启动VS并再次构建?可能您的SetContentView方法错误,请尝试将其与R.layout.main一起使用。像这样SetContentView(R.layout.main)@LukeVo重新启动VS并重建没有帮助。谢谢!导出它并添加该文件正是问题所在!很乐意帮忙:D
public class Main: AppcompatActivity, View.IonClickListener
public void testClick(View view)
{
//Show alert saying clicked...
}