C# Xamarin OnClick function

前端 未结 3 421

What I\'m doing is this

Button button1 = FindViewById
3条回答
  •  执笔经年
    2021-01-02 08:31

    The real issue here is in your SetOnClickListener you are setting an inline anonymous class implementing OnClickListener interface.

    This is not supported in C#, From the C# programming guide, you can find,

    Anonymous types are class types that consist of one or more public read-only properties. No other kinds of class members such as methods or events are allowed. An anonymous type cannot be cast to any interface or type except for object.

    But it doesn't mean that you cannot use SetOnClickListener at all.

    You can either do button1.SetOnClickListener(this) and implement your OnClickListener in the same class

    or

    create a class (can be inner class) implements OnClickListener with your implementation and pass an it's instance to your SetOnClickListener

    In both ways, your are obeying C#'s "Real Name Policy" :)

提交回复
热议问题