【问题标题】:Overriding ListView property in Monodroid在 Monodroid 中覆盖 ListView 属性
【发布时间】:2012-10-01 09:55:28
【问题描述】:

我目前正在 Monodroid 中编程,但我遇到了扩展 Listview 的问题。

我目前的 ListView 扩展如下:

public class TTListView : ListView
{
    private Context mContext;
    private bool wrapAdapter;

    public TTListView(Context context) :
        base(context)
    {
        Initialize();
        this.mContext = context;
    }

    public TTListView(Context context, IAttributeSet attrs) :
        base(context, attrs)
    {
        Initialize();
        this.mContext = context;
    }

    public TTListView(Context context, IAttributeSet attrs, int defStyle) :
        base(context, attrs, defStyle)
    {
        Initialize();
        this.mContext = context;
    }

    private void Initialize()
    {
        this.CacheColorHint = Color.Transparent;
        //Still some more stuff to be added here
    }

    public void InsertItemAt(int index)
    {
        Animation anim = AnimationUtils.LoadAnimation(
                 mContext, Resource.Animator.slide_top_down);
        anim.Duration = 500;
        this.GetChildAt(index).StartAnimation(anim);
    }

    public void SetDelegate(TTListDelegate _delegate)
    {
        this.OnItemClickListener = (IOnItemClickListener)_delegate;
        this.OnItemLongClickListener = (IOnItemLongClickListener)_delegate;
    }

    public override void AddFooterView(View v)
    {
        base.AddFooterView(v);
        wrapAdapter = true;
    }

    /*public override IListAdapter Adapter
    {
        get
        {
            return base.Adapter;
        }
        set
        {
            //Check if the passed parameter is a TTListAdapter
            TTListAdapter _ttadapter = value as TTListAdapter;
            if (_ttadapter != null)
            {
                _ttadapter.Wrapped = wrapAdapter;
            }
            base.Adapter = value;
        }
    }*/
}

上面的代码工作得很好。 问题在于,当我尝试覆盖 Adapter 属性(现已注释掉)时,尝试创建 TTListView 对象时出现以下异常:

"Unable to activate instance of type TimeTellApp.TTListView from native handle 40557188. No constructor found for TTListView::.ctor(System.IntPtr, Android.Runtime.JniHandleOwner)"

通常这与 GC 销毁托管映射对象有关,所以到目前为止,我通过保留对对象的引用来解决这些问题。 TTListView的问题是调用构造函数进行初始化时已经出现异常。

我像这样创建一个 TTListView 对象:

TTListView setting_listview = new TTListView(this);

(这是Activity) 这可能是什么问题以及解决问题的最佳方法是什么?

【问题讨论】:

    标签: android listview xamarin.android extend


    【解决方案1】:

    错误消息表明你的类中缺少一个特定的构造函数,你应该实现它:

    protected ListView (IntPtr javaReference, Android.Runtime.JniHandleOwnership transfer)

    【讨论】:

    • 这行得通。谢谢!仍然想知道为什么这个实现需要它,而我正在扩展的其他 View 类不需要它。
    猜你喜欢
    • 1970-01-01
    • 2013-07-19
    • 2018-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    相关资源
    最近更新 更多