问题
My code is
namespace classlibrary
{
public class numerictext : EditText
{
//My code
}
When I try to inherit a edit text control in a class library, I'm getting the error: Parent does not contain a constructor that takes 0 arguments. I understand the problem is that Parent has no constructor with 0 arguments. But how to inherit a control in a class library?
回答1:
You need to call one of the base constructors in your derived class. Assuming you are using the Android EditText widget:
public class numerictext : EditText
{
public numerictext(Context context) : base(context)
//^^^^^^^^^^^^^^^
//Note how we are now calling the base constructure
{
//empty or you can add your own code
}
public numerictext(Context context, IAttributeSet attrs) : base(context, attrs)
{
}
//etc
}
回答2:
For any Android-based View subclass, you need to supply the three constructors that call their base object constructors, ie.:
public class MyView : EditText
{
public MyView(Context context) : base(context) { }
public MyView(Context context, IAttributeSet attrs) : base(context, attrs) { }
public MyView(Context context, IAttributeSet attrs, int defStyle) : base(context, attrs, defStyle) { }
// your override code....
}
回答3:
Have you considered changing EditText to:
public class EditTtext
{
data stuff..
EditText() {}
other functions
}
Though I don't see why yours wouldn't work as is
来源:https://stackoverflow.com/questions/51233923/parent-does-not-contain-a-constructor-that-takes-0-arguments