Xamarin xml android:onClick 回调方法 [英] Xamarin xml android:onClick callback method

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

问题描述

这是我的 XML 代码:

This is my XML code:

    <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"函数,但正如您所看到的,我根据文档声明了所有内容.

Problem is I get a runtime error and the debug window complains that Buttoncould not find "sayHellow" function, but as you can see I declared everything according to the docs.

推荐答案

必须导出方法:

[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
};

这篇关于Xamarin xml android:onClick 回调方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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