Android MonoDroid onClick From XML没有这样的方法错误

Android MonoDroid onClick From XML没有这样的方法错误,android,xamarin.android,Android,Xamarin.android,LiftInspection.axml <Button android:id="@+id/expandCollapse1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:drawableLeft="@drawable/expand_arrow" android:text="Function/Cont

LiftInspection.axml

<Button
      android:id="@+id/expandCollapse1"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"          
      android:drawableLeft="@drawable/expand_arrow"
      android:text="Function/Controls"
      android:textSize="20sp"
      android:textColor="@android:color/white"
      android:background="@drawable/expandCollapseButton"
      android:gravity="center"
      android:onClick="button_Click"/>
我一单击按钮,应用程序就会崩溃并强制关闭。在Android日志中,我发现“java.lang.IllegalStateException:在id为“expandCollapse1”的视图类Android.widget.button上的活动类CPECFieldDapp.LiftInspection for onClick处理程序中找不到方法按钮(视图)”


我在从xml设置单击事件时发现的一切只显示了我正在做的事情。将其放入android:onClick from XML中,并具有一个public void,其中唯一的参数是实现该布局的活动中的视图。我遗漏了什么?

这在当前版本的Android Mono中不受支持。有关它的更多详细信息,请参阅。

我通过将
[Export]
属性添加到您的
按钮(单击
方法)来实现这一点。

现在这是可能的。你需要做三件事:

  • 参考
    Mono.Android.Export
  • 使用Java.Interop
  • 对事件方法进行如下注释:
  • [导出(“按钮点击”)]
    公共作废按钮\单击(查看)
    {  
    Toast.MakeText(这是“测试”,ToastLength.Long).Show();
    }  
    
    即将发布的4.1.0 alpha版本将支持此方案。
    protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
    
            SetContentView(Resource.Layout.LiftInspection);
        }
    
    public void button_Click(View view)
        {
            Toast.MakeText(this, "Testing", ToastLength.Long).Show();           
        }