【问题标题】:Parent does not contain a constructor that takes 0 argumentsParent 不包含带 0 个参数的构造函数
【发布时间】:2018-07-08 16:21:39
【问题描述】:

我的代码是

 namespace classlibrary
 {
   public class numerictext : EditText
    {
      //My code

    } 

当我尝试在类库中继承编辑文本控件时,我收到错误消息:Parent 不包含采用 0 个参数的构造函数。我知道问题是 Parent 没有带有 0 个参数的构造函数。但是如何继承类库中的控件呢?

【问题讨论】:

  • 你的 numerictext 有构造函数吗?它看起来像什么?

标签: c# android xamarin uwp


【解决方案1】:

对于任何基于 Android 的 View 子类,您需要提供调用其基对象构造函数的三个构造函数,即:

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....
}

【讨论】:

  • 从技术上讲,有 5 个构造函数,但您不需要全部提供。
  • @DavidG 是的,你是对的,但列出的三个是在基于Xamarin.Android 的 JNI/NDK 应用程序中使用的那些
【解决方案2】:

您需要在派生类中调用其中一个基本构造函数。假设您使用的是 Android EditText 小部件:

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
} 

【讨论】:

    【解决方案3】:

    您是否考虑过将 EditText 更改为:

    public class EditTtext 
    {
       data stuff..
       EditText() {}
    
       other functions
    }
    

    虽然我不明白为什么你的不能按原样工作

    【讨论】:

    • 我想在android中创建数字文本框。我需要通过继承编辑文本在类库中创建控件。当我尝试继承时,我得到了错误。
    • 他使用的 EditText 类是一个 Xamarin Forms 控件。他需要子类化一个控件,并为此调用正确的基本构造函数。
    猜你喜欢
    • 2013-02-16
    • 2012-08-21
    • 2011-11-06
    • 1970-01-01
    • 1970-01-01
    • 2014-04-14
    • 2012-01-01
    • 2013-02-17
    • 1970-01-01
    相关资源
    最近更新 更多