这是我的 XML 代码:
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/MyButton"
android:id="@+id/button1"
android:onClick="sayHellow" /> //RELEVANT PART
这是我的主要活动:
[Activity(Label = "FFFF", MainLauncher = true, Icon = "@drawable/icon", Theme = "@style/Theme.AppCompat.Light")]
public class MainActivity : AppCompatActivity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
}
public void sayHellow(View v) //CALLBACK FUNCTION
{
Snackbar.Make(v, "My text", Snackbar.LengthLong)
.Show();
}
}
问题是我遇到运行时错误并且调试窗口抱怨说Button
找不到“sayHellow”函数,但正如你所看到的,我根据文档声明了所有内容。
您必须导出该方法:
[Export("sayHellow")]
public void sayHellow(View v)
{
Snackbar.Make(v, "My text", Snackbar.LengthLong).Show();
}
并且您必须添加对Java.Interop.dll
一个更简单的解决方案是:
Button button = FindViewById<Button> (Resource.Id.myButton);
button.Click += delegate {
//Clicked
};
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)